### Install Adapters from Local Repository Clone Source: https://docs.adapterhub.ml/_sources/installation.md.txt Clone the repository and install the package from source. This method allows for running included example scripts and is suitable for development. ```bash git clone https://github.com/adapter-hub/adapters.git cd adapters pip install . ``` -------------------------------- ### Load Adapter Setup Source: https://docs.adapterhub.ml/classes/models/plbart.html Loads an adapter setup from a local or remote source. Can optionally activate the loaded setup. ```python load_adapter_setup(_adapter_setup_name_or_path : str_, _version : Optional[str] = None_, _custom_weights_loaders : Optional[List[WeightsLoader]] = None_, _set_active : bool = False_, _use_safetensors : bool = False_, _** kwargs_) → str Loads an adapter setup from the local file system or a remote location. Parameters * **adapter_setup_name_or_path** (_str_) can be either: * the identifier of a repository on the HuggingFace Model Hub. * a path to a directory containing adapter weights saved using model.save_adapter_setup() * a URL pointing to a zip folder containing a saved adapter module * **version** (_str_ _,__optional_) – The version of the adapter to be loaded. * **set_active** (_bool_ _,__optional_) – Set the loaded adapter setup to be the active one. By default (False), the adapter setup is loaded but not activated. * **use_safetensors** (_bool_ _,__optional_) – If True, weights are loaded via safetensors if safetensors checkpoint is available. Otherwise, the regular torch save method is used. Returns The loaded adapter setup and the head setup if available. Return type Tuple[AdapterCompositionBlock, Any] ``` -------------------------------- ### Install adapters and transformers libraries Source: https://docs.adapterhub.ml/transitioning.html Install the 'adapters' library, which will automatically install a compatible version of 'transformers'. This ensures both libraries are available in the same environment. ```bash pip install adapters ``` -------------------------------- ### Install AdapterHub and Requirements Source: https://docs.adapterhub.ml/training.html Clone the AdapterHub repository and install the library along with example-specific requirements. Ensure you are in the correct directory after cloning. ```bash git clone https://github.com/adapter-hub/adapters cd adapters pip install . pip install -r ./examples/pytorch//requirements.txt ``` -------------------------------- ### Install Adapters from PyPI Source: https://docs.adapterhub.ml/_sources/installation.md.txt Use this command to install the stable version of the adapters library from the Python Package Index. ```bash pip install adapters ``` -------------------------------- ### Install AdapterHub and Development Dependencies Source: https://docs.adapterhub.ml/_sources/contributing.md.txt Install the 'adapters' library in editable mode along with its development dependencies. ```bash pip install -e ".[dev]" ``` -------------------------------- ### Save and Load Adapter Fusion Setup Source: https://docs.adapterhub.ml/_sources/loading.md.txt Saves an AdapterFusion setup with prediction heads to the local file system and demonstrates how to push it to the Model Hub. It also includes a commented-out example for re-loading the setup. ```python from transformers import AutoAdapterModel from adapter_transformers import SeqBnConfig, Fuse # Create an AdapterFusion model = AutoAdapterModel.from_pretrained("bert-base-uncased") model.load_adapter("sentiment/sst-2@ukp", config=SeqBnConfig(), with_head=False) model.load_adapter("nli/multinli@ukp", config=SeqBnConfig(), with_head=False) model.load_adapter("sts/qqp@ukp", config=SeqBnConfig(), with_head=False) model.add_adapter_fusion(["sst-2", "mnli", "qqp"]) model.add_classification_head("clf_head") adapter_setup = Fuse("sst-2", "mnli", "qqp") head_setup = "clf_head" model.set_active_adapters(adapter_setup) model.active_head = head_setup # Train AdapterFusion ... # Save model.save_adapter_setup("checkpoint", adapter_setup, head_setup=head_setup) # Push to Hub model.push_adapter_setup_to_hub("/fusion_setup", adapter_setup, head_setup=head_setup) # Re-load # model.load_adapter_setup("checkpoint", set_active=True) ``` -------------------------------- ### push_adapter_setup_to_hub Source: https://docs.adapterhub.ml/classes/model_mixins.html Uploads an adapter setup, including configuration and potentially head setup, to HuggingFace's Model Hub. This method is useful for uploading a complete adapter composition. ```APIDOC ## push_adapter_setup_to_hub ### Description Upload an adapter setup to HuggingFace’s Model Hub. ### Method (Not specified, likely a Python method call) ### Parameters #### Path Parameters (None specified) #### Query Parameters (None specified) #### Request Body (None specified) ### Parameters * **repo_id** (str) - Required - The name of the repository on the model hub to upload to. * **adapter_setup** (Union[str, list, AdapterCompositionBlock]) - Required - The adapter setup to be uploaded. Usually an adapter composition block. * **head_setup** (Optional[Union[bool, str, list, AdapterCompositionBlock]]) - Optional - The head setup to be uploaded. * **datasets_tag** (str) - Optional - Dataset identifier from https://huggingface.co/datasets. Defaults to None. * **local_path** (str) - Optional - Local path used as clone directory of the adapter repository. If not specified, will create a temporary directory. Defaults to None. * **commit_message** (str) - Optional - Message to commit while pushing. Will default to "add config", "add tokenizer" or "add model" depending on the type of the class. * **private** (bool) - Optional - Whether or not the repository created should be private (requires a paying subscription). * **token** (Union[bool, str]) - Optional - The token to use as HTTP bearer authorization for remote files. If True, will use the token generated when running huggingface-cli login (stored in ~/.huggingface). Will default to True if repo_url is not specified. * **overwrite_adapter_card** (bool) - Optional - Overwrite an existing adapter card with a newly generated one. If set to False, will only generate an adapter card, if none exists. Defaults to False. * **create_pr** (bool) - Optional - Whether or not to create a PR with the uploaded files or directly commit. * **revision** (str) - Optional - Branch to push the uploaded files to. * **commit_description** (str) - Optional - The description of the commit that will be created * **adapter_card_kwargs** (Optional[dict]) - Optional - Additional arguments to pass to the adapter card text generation. Currently includes: tags, language, license, metrics, architecture_training, results, citation. ### Returns str - The url of the adapter repository on the model hub. ``` -------------------------------- ### load_adapter_setup Source: https://docs.adapterhub.ml/classes/models/deberta_v2.html Loads an adapter setup from the local file system or a remote location. The adapter setup can be loaded from the HuggingFace Model Hub, a local directory, or a URL. ```APIDOC ## load_adapter_setup ### Description Loads an adapter setup from the local file system or a remote location. ### Method POST ### Endpoint /load_adapter_setup ### Parameters #### Path Parameters * **adapter_setup_name_or_path** (str) - Required - Can be either: * the identifier of a repository on the HuggingFace Model Hub. * a path to a directory containing adapter weights saved using model.save_adapter_setup() * a URL pointing to a zip folder containing a saved adapter module * **version** (str, _optional_) – The version of the adapter to be loaded. * **set_active** (bool, _optional_) – Set the loaded adapter setup to be the active one. By default (False), the adapter setup is loaded but not activated. * **use_safetensors** (bool, _optional_) – If True, weights are loaded via safetensors if safetensors checkpoint is available. Otherwise, the regular torch save method is used. ### Returns * **str** – The name with which the adapter setup was added to the model. ``` -------------------------------- ### Install Latest Development Version from GitHub Source: https://docs.adapterhub.ml/_sources/installation.md.txt Install the most recent development version directly from the adapters GitHub repository. This is useful for accessing the latest features or bug fixes. ```bash pip install git+https://github.com/adapter-hub/adapters.git ``` -------------------------------- ### load_adapter_setup Source: https://docs.adapterhub.ml/classes/models/vit.html Loads an adapter setup from the local file system or a remote location. Supports loading from HuggingFace Model Hub identifiers, local directories, or URLs to zip folders, with options to specify version, activate the setup, and use safetensors. ```APIDOC ## load_adapter_setup ### Description Loads an adapter setup from the local file system or a remote location. ### Method load_adapter_setup ### Parameters #### Path Parameters * **adapter_setup_name_or_path** (str) - Required - Can be either: the identifier of a repository on the HuggingFace Model Hub, a path to a directory containing adapter weights saved using model.save_adapter_setup(), or a URL pointing to a zip folder containing a saved adapter module. * **version** (str) - Optional - The version of the adapter to be loaded. * **set_active** (bool) - Optional - Set the loaded adapter setup to be the active one. By default (False), the adapter setup is loaded but not activated. * **use_safetensors** (bool) - Optional - If True, weights are loaded via safetensors if safetensors checkpoint is available. Otherwise, the regular torch save method is used. ### Returns The loaded adapter setup and the head setup if available. ### Return type Tuple[AdapterCompositionBlock, Any] ``` -------------------------------- ### load_adapter_setup Source: https://docs.adapterhub.ml/classes/models/bert-generation.html Loads an adapter setup from a local file system or a remote location, with options for specifying versions and activation. ```APIDOC ## load_adapter_setup ### Description Loads an adapter setup from the local file system or a remote location. ### Parameters * **adapter_setup_name_or_path** (str) - Can be either: * **version** (str, optional) - The version of the adapter setup to be loaded. * **custom_weights_loaders** (Optional[List[WeightsLoader]]) * **set_active** (bool, optional) - Set the loaded adapter setup to be the active one. By default (False), the adapter setup is loaded but not activated. * **use_safetensors** (bool, optional) - If True, weights are loaded via safetensors if safetensors checkpoint is available. Otherwise, the regular torch save method is used. ### Returns The name with which the adapter setup was added to the model. ### Return type str ``` -------------------------------- ### push_adapter_setup_to_hub Source: https://docs.adapterhub.ml/classes/models/vit.html Uploads an adapter setup to HuggingFace’s Model Hub. Supports specifying the repository ID, adapter setup details, head setup, datasets tag, local path, commit message, and privacy settings. ```APIDOC ## push_adapter_setup_to_hub ### Description Upload an adapter setup to HuggingFace’s Model Hub. ### Method push_adapter_setup_to_hub ### Parameters #### Path Parameters * **repo_id** (str) - Required - The name of the repository on the model hub to upload to. * **adapter_setup** (Union[str, list, AdapterCompositionBlock]) - Required - The adapter setup to be uploaded. Usually an adapter composition block. * **head_setup** (Optional[Union[bool, str, list, AdapterCompositionBlock]]) - Optional - The head setup to be uploaded. * **datasets_tag** (Optional[str]) - Optional - A tag for the datasets used. * **local_path** (Optional[str]) - Optional - Path to the local adapter setup. * **commit_message** (Optional[str]) - Optional - Commit message for the upload. * **private** (Optional[bool]) - Optional - Whether the repository should be private. * **token** (Optional[Union[bool, str]]) - Optional - Authentication token for HuggingFace Hub. * **overwrite_adapter_card** (bool) - Optional - Whether to overwrite the adapter card. Defaults to False. * **create_pr** (bool) - Optional - Whether to create a pull request. Defaults to False. * **revision** (Optional[str]) - Optional - The revision of the repository. * **commit_description** (Optional[str]) - Optional - Description for the commit. * **adapter_card_kwargs** (Optional[dict]) - Optional - Additional keyword arguments for the adapter card. ``` -------------------------------- ### push_adapter_setup_to_hub Source: https://docs.adapterhub.ml/classes/models/mistral.html Uploads an adapter setup to the HuggingFace Model Hub. This method allows specifying the repository ID, the adapter and head setups to upload, and optional parameters for commit messages, privacy, and overwriting existing content. ```APIDOC ## push_adapter_setup_to_hub ### Description Upload an adapter setup to HuggingFace’s Model Hub. ### Method push_adapter_setup_to_hub ### Parameters #### Path Parameters * **repo_id** (str) - Required - The name of the repository on the model hub to upload to. * **adapter_setup** (Union[str, list, AdapterCompositionBlock]) - Required - The adapter setup to be uploaded. Usually an adapter composition block. * **head_setup** (Optional[Union[bool, str, list, AdapterCompositionBlock]]) - Optional - The head setup to be uploaded. * **datasets_tag** (str) - Optional - Tag for datasets. * **local_path** (str) - Optional - Local path to the adapter setup. * **commit_message** (str) - Optional - Commit message for the upload. * **private** (bool) - Optional - Whether the repository should be private. * **token** (Union[bool, str]) - Optional - Authentication token for the HuggingFace Hub. * **overwrite_adapter_card** (bool) - Optional - Whether to overwrite the adapter card. * **create_pr** (bool) - Optional - Whether to create a pull request. * **revision** (str) - Optional - The revision of the repository. * **commit_description** (str) - Optional - Description for the commit. * **adapter_card_kwargs** (dict) - Optional - Additional keyword arguments for the adapter card. ``` -------------------------------- ### Save Adapter Setup Source: https://docs.adapterhub.ml/classes/models/plbart.html Saves a defined adapter setup, which can include multiple adapters and head configurations, to a specified directory. This facilitates sharing and reloading complex adapter compositions. ```python model.save_adapter_setup("/path/to/save/setup", adapter_setup=["adapter1", "adapter2"], head_setup=True) ``` -------------------------------- ### WhisperAdapterModel.push_adapter_setup_to_hub Source: https://docs.adapterhub.ml/classes/models/whisper.html Uploads an adapter setup and optionally a head setup to the Hugging Face Model Hub. This method facilitates sharing and versioning of adapter configurations. ```APIDOC ## WhisperAdapterModel.push_adapter_setup_to_hub ### Description Upload an adapter setup to HuggingFace’s Model Hub. ### Parameters * **repo_id** (str) - The name of the repository on the model hub to upload to. * **adapter_setup** (Union[str, list, AdapterCompositionBlock]) - The adapter setup to be uploaded. Usually an adapter composition block. * **head_setup** (Optional[Union[bool, str, list, AdapterCompositionBlock]], optional) - The head setup to be uploaded. * **datasets_tag** (str, optional) - Dataset identifier from https://huggingface.co/datasets. Defaults to None. * **local_path** (str, optional) - Local path used as clone directory of the adapter repository. If not specified, will create a temporary directory. Defaults to None. * **commit_message** (str, optional) - Message to commit while pushing. Will default to `"add config"`, `"add tokenizer"` or `"add model"` depending on the type of the class. * **private** (bool, optional) - Whether or not the repository created should be private (requires a paying subscription). * **token** (bool or str, optional) - The token to use as HTTP bearer authorization for remote files. If True, will use the token generated when running huggingface-cli login (stored in ~/.huggingface). Will default to True if repo_url is not specified. * **overwrite_adapter_card** (bool, optional) - Overwrite an existing adapter card with a newly generated one. If set to False, will only generate an adapter card, if none exists. Defaults to False. * **create_pr** (bool, optional) - Whether or not to create a PR with the uploaded files or directly commit. * **revision** (str, optional) - Branch to push the uploaded files to. * **commit_description** (str, optional) - The description of the commit that will be created * **adapter_card_kwargs** (Optional[dict], optional) - Additional arguments to pass to the adapter card text generation. Currently includes: tags, language, license, metrics, architecture_training, results, citation. ### Returns The url of the adapter repository on the model hub. ### Return type str ``` -------------------------------- ### save_adapter_setup Source: https://docs.adapterhub.ml/classes/models/beit.html Saves an adapter setup configuration to a directory, enabling it to be reloaded later with load_adapter_setup(). ```APIDOC ## save_adapter_setup ### Description Saves an adapter setup to a directory so that it can be shared or reloaded using load_adapter_setup(). ### Method POST ### Endpoint /save_adapter_setup ### Parameters #### Path Parameters - **save_directory** (str) - Required - Path to a directory where the adapter setup should be saved. - **adapter_setup** (Union[str, list, AdapterCompositionBlock]) - Required - The adapter setup to be saved. Usually an adapter composition block. #### Query Parameters - **head_setup** (Optional[Union[bool, str, list, AdapterCompositionBlock]]) - Optional - The head setup to be saved. Can be either: True: save the default head for models without flex heads. str: save a single head with the given name. list: save a list of heads. AdapterCompositionBlock: save a custom head setup. None (default): do not save any heads. - **use_safetensors** (bool) - Optional - If True, weights are saved via safetensors. Otherwise, the regular torch save method is used. ### Request Example ```json { "save_directory": "/path/to/save", "adapter_setup": ["adapter1", "adapter2"], "head_setup": true, "use_safetensors": false } ``` ### Response #### Success Response (200) - **message** (str) - Indicates successful saving of the adapter setup. #### Response Example ```json { "message": "Adapter setup saved successfully." } ``` ``` -------------------------------- ### BartAdapterModel.push_adapter_setup_to_hub Source: https://docs.adapterhub.ml/classes/models/bart.html Uploads an adapter setup, and optionally a head setup, to the Hugging Face Model Hub. This method facilitates sharing and versioning of adapter configurations. ```APIDOC ## BartAdapterModel.push_adapter_setup_to_hub ### Description Upload an adapter setup to HuggingFace’s Model Hub. ### Method push_adapter_setup_to_hub ### Parameters #### Path Parameters - **repo_id** (str) - Required - The name of the repository on the model hub to upload to. #### Optional Parameters - **adapter_setup** (Union[str, list, AdapterCompositionBlock]) - Required - The adapter setup to be uploaded. Usually an adapter composition block. - **head_setup** (Optional[Union[bool, str, list, AdapterCompositionBlock]]) - Optional - The head setup to be uploaded. - **datasets_tag** (str) - Optional - Dataset identifier from https://huggingface.co/datasets. Defaults to None. - **local_path** (str) - Optional - Local path used as clone directory of the adapter repository. If not specified, will create a temporary directory. Defaults to None. - **commit_message** (str) - Optional - Message to commit while pushing. Will default to "add config", "add tokenizer" or "add model" depending on the type of the class. - **private** (bool) - Optional - Whether or not the repository created should be private (requires a paying subscription). - **token** (Union[bool, str]) - Optional - The token to use as HTTP bearer authorization for remote files. If True, will use the token generated when running huggingface-cli login (stored in ~/.huggingface). Will default to True if repo_url is not specified. - **overwrite_adapter_card** (bool) - Optional - Defaults to False. - **create_pr** (bool) - Optional - Defaults to False. - **revision** (str) - Optional - Defaults to None. - **commit_description** (str) - Optional - Defaults to None. - **adapter_card_kwargs** (dict) - Optional - Defaults to None. ``` -------------------------------- ### Install Transformers from Local Git Submodule Source: https://docs.adapterhub.ml/_sources/contributing.md.txt Install the Hugging Face Transformers library from its local git submodule within the AdapterHub project. ```bash pip install ./hf_transformers ``` -------------------------------- ### push_adapter_setup_to_hub Source: https://docs.adapterhub.ml/classes/models/beit.html Uploads an adapter setup to HuggingFace's Model Hub. This method supports specifying the repository, adapter setup details, datasets tag, local path, commit message, and privacy settings. ```APIDOC ## push_adapter_setup_to_hub ### Description Uploads an adapter setup to HuggingFace's Model Hub. ### Method push_adapter_setup_to_hub ### Parameters #### Path Parameters * **repo_id** (str) - The name of the repository on the model hub to upload to. * **adapter_setup** (Union[str, list, AdapterCompositionBlock]) - The adapter setup to upload. * **head_setup** (Optional[Union[bool, str, list, AdapterCompositionBlock]]) - The head setup to upload. * **datasets_tag** (Optional[str]) - Tag for datasets. * **local_path** (Optional[str]) - Local path to the adapter setup. * **commit_message** (Optional[str]) - Commit message for the upload. * **private** (Optional[bool]) - Whether the repository should be private. * **token** (Optional[Union[bool, str]]) - Authentication token. * **overwrite_adapter_card** (bool, optional) - Whether to overwrite the adapter card. Defaults to False. * **create_pr** (bool, optional) - Whether to create a pull request. Defaults to False. * **revision** (Optional[str]) - The revision of the repository. * **commit_description** (Optional[str]) - Description for the commit. * **adapter_card_kwargs** (Optional[dict]) - Additional arguments for the adapter card. ``` -------------------------------- ### Add Adapter with Configuration Source: https://docs.adapterhub.ml/_sources/overview.md.txt Example of adding an adapter to a model instance using a configuration object. ```python model.add_adapter("name", config=) ``` -------------------------------- ### Example of Setting a Submodule Source: https://docs.adapterhub.ml/classes/models/plbart.html Demonstrates how to override a specific submodule (e.g., Conv2d) with a new one (e.g., Linear) or add a new submodule. ```python A( (net_b): Module( (net_c): Module( (conv): Conv2d(3, 3, 3) ) (linear): Linear(3, 3) ) ) set_submodule("net_b.net_c.conv", nn.Linear(1, 1)) set_submodule("net_b.conv", nn.Conv2d(1, 1, 1)) ``` -------------------------------- ### Saving and Loading Adapter Compositions Source: https://docs.adapterhub.ml/loading.html Illustrates the process of creating, saving, pushing to the hub, and re-loading an AdapterFusion setup with multiple adapters and a classification head. ```python # Create an AdapterFusion model = AutoAdapterModel.from_pretrained("bert-base-uncased") model.load_adapter("sentiment/sst-2@ukp", config=SeqBnConfig(), with_head=False) model.load_adapter("nli/multinli@ukp", config=SeqBnConfig(), with_head=False) model.load_adapter("sts/qqp@ukp", config=SeqBnConfig(), with_head=False) model.add_adapter_fusion(["sst-2", "mnli", "qqp"]) model.add_classification_head("clf_head") adapter_setup = Fuse("sst-2", "mnli", "qqp") head_setup = "clf_head" model.set_active_adapters(adapter_setup) model.active_head = head_setup # Train AdapterFusion ... # Save model.save_adapter_setup("checkpoint", adapter_setup, head_setup=head_setup) # Push to Hub model.push_adapter_setup_to_hub("/fusion_setup", adapter_setup, head_setup=head_setup) # Re-load # model.load_adapter_setup("checkpoint", set_active=True) ``` -------------------------------- ### setup_adapter_training Source: https://docs.adapterhub.ml/classes/adapter_training.html Sets up the model for adapter training using the provided adapter arguments. This function configures the model based on the specified adapter configurations and names. ```APIDOC ## setup_adapter_training ### Description Setup model for adapter training based on given adapter arguments. ### Parameters - **model** (__type__) - The model instance to be trained. - **adapter_args** (AdapterArguments) - The adapter arguments used for configuration. - **adapter_name** (str) - The name of the adapter to be added. - **adapter_config_kwargs** (Optional[dict]) - Additional keyword arguments for adapter configuration. - **adapter_load_kwargs** (Optional[dict]) - Additional keyword arguments for loading the adapter. ### Returns A tuple containing the names of the loaded adapters. ### Return Type Tuple[str, str] ``` -------------------------------- ### push_adapter_setup_to_hub Source: https://docs.adapterhub.ml/classes/models/gptj.html Uploads a complete adapter setup, including configuration and potentially heads, to the Hugging Face Model Hub. This method allows for detailed control over repository creation, commit messages, and adapter card generation. ```APIDOC ## push_adapter_setup_to_hub ### Description Upload an adapter setup to HuggingFace’s Model Hub. ### Method (Not specified, likely a Python method call) ### Endpoint (Not applicable, this is an SDK method) ### Parameters #### Path Parameters (None) #### Query Parameters (None) #### Request Body (None) ### Parameters * **repo_id** (str) - Required - The name of the repository on the model hub to upload to. * **adapter_setup** (Union[str, list, AdapterCompositionBlock]) - Required - The adapter setup to be uploaded. Usually an adapter composition block. * **head_setup** (Optional[Union[bool, str, list, AdapterCompositionBlock]]) - Optional - The head setup to be uploaded. * **datasets_tag** (Optional[str]) - Optional - Dataset identifier from https://huggingface.co/datasets. Defaults to None. * **local_path** (Optional[str]) - Optional - Local path used as clone directory of the adapter repository. If not specified, will create a temporary directory. Defaults to None. * **commit_message** (Optional[str]) - Optional - Message to commit while pushing. Will default to "add config", "add tokenizer" or "add model" depending on the type of the class. * **private** (Optional[bool]) - Optional - Whether or not the repository created should be private (requires a paying subscription). * **token** (Optional[Union[bool, str]]) - Optional - The token to use as HTTP bearer authorization for remote files. If True, will use the token generated when running huggingface-cli login (stored in ~/.huggingface). Will default to True if repo_url is not specified. * **overwrite_adapter_card** (bool) - Optional - Overwrite an existing adapter card with a newly generated one. If set to False, will only generate an adapter card, if none exists. Defaults to False. * **create_pr** (bool) - Optional - Whether or not to create a PR with the uploaded files or directly commit. * **revision** (Optional[str]) - Optional - Branch to push the uploaded files to. * **commit_description** (Optional[str]) - Optional - The description of the commit that will be created * **adapter_card_kwargs** (Optional[dict]) - Optional - Additional arguments to pass to the adapter card text generation. Currently includes: tags, language, license, metrics, architecture_training, results, citation. ### Returns The url of the adapter repository on the model hub. ### Return Type str ``` -------------------------------- ### Using AdapterSetup as a Context Manager Source: https://docs.adapterhub.ml/classes/adapter_config.html Demonstrates how to use AdapterSetup to temporarily activate a stack of adapters within a 'with' statement. This is useful for applying specific adapter configurations to a model for a limited scope. ```python with AdapterSetup(Stack("a", "b")): # will use the adapter stack "a" and "b" outputs = model(**inputs) ``` -------------------------------- ### Get Adapter Configuration Source: https://docs.adapterhub.ml/classes/model_adapters_config.html Retrieve the configuration dictionary for a specific adapter by its name using the `get` method. Returns `None` if the adapter is not found. ```python model_adapters_config.get(adapter_name="adapter_name") ``` -------------------------------- ### load_adapter_setup Source: https://docs.adapterhub.ml/classes/models/bert-generation.html Loads an adapter setup from a repository identifier, local path, or URL. It supports optional parameters for version, setting the adapter as active, and using safetensors for loading. ```APIDOC ## load_adapter_setup ### Description Loads an adapter setup from a repository identifier, local path, or URL. It supports optional parameters for version, setting the adapter as active, and using safetensors for loading. ### Parameters * **repo_id** (_str_ or _pathlib.Path_ or _str_) - The identifier of a repository on the HuggingFace Model Hub, a path to a directory containing adapter weights, or a URL pointing to a zip folder. * **version** (_str_, optional) – The version of the adapter to be loaded. * **set_active** (_bool_, optional) – Set the loaded adapter setup to be the active one. By default (False), the adapter setup is loaded but not activated. * **use_safetensors** (_bool_, optional) – If True, weights are loaded via safetensors if safetensors checkpoint is available. Otherwise, the regular torch save method is used. ### Returns The loaded adapter setup and the head setup if available. ### Return type Tuple[AdapterCompositionBlock, Any] ``` -------------------------------- ### Upload Adapter Setup to Hub Source: https://docs.adapterhub.ml/classes/models/plbart.html Uploads a configured adapter setup to the Hugging Face Model Hub. This allows sharing and versioning of adapter configurations. ```python model.push_adapter_setup_to_hub(repo_id="my-repo", adapter_setup=my_adapter_setup) ``` -------------------------------- ### Add, activate, and train adapters Source: https://docs.adapterhub.ml/_sources/transitioning.md.txt Demonstrates the basic adapter operations: adding a new adapter with a specified configuration, activating it for inference, and preparing the model for training with a specific adapter. ```python # add adapter to the model model.add_adapter("adapter_name", config="lora") # activate adapter model.set_active_adapters("adapter_name") # freeze model weights and activate adapter model.train_adapter("adapter_name") ``` -------------------------------- ### load_adapter_setup Source: https://docs.adapterhub.ml/classes/models/beit.html Loads an adapter setup from the local file system or a remote location. This method supports loading from HuggingFace Model Hub identifiers, local directories, or URLs, with options to set it as active and use safetensors. ```APIDOC ## load_adapter_setup ### Description Loads an adapter setup from the local file system or a remote location. ### Method load_adapter_setup ### Parameters #### Path Parameters * **adapter_setup_name_or_path** (str) - Can be a HuggingFace Model Hub identifier, a path to a local directory, or a URL to a zip folder. * **version** (str, optional) - The version of the adapter to be loaded. * **custom_weights_loaders** (List[WeightsLoader], optional) - Custom weights loaders. * **set_active** (bool, optional) - Whether to set the loaded adapter setup as active. Defaults to False. * **use_safetensors** (bool, optional) - If True, weights are loaded via safetensors if available. Defaults to False. ### Returns * **Tuple[AdapterCompositionBlock, Any]** - The loaded adapter setup and the head setup if available. ``` -------------------------------- ### Manually Set Up Task Adapter Source: https://docs.adapterhub.ml/_sources/training.md.txt This snippet demonstrates how to manually add and configure a task adapter, then activate it for training. Ensure the adapter is not already configured before adding. ```python # task adapter - only add if not existing if task_name not in model.adapters_config: # resolve the adapter config adapter_config = AdapterConfig.load(adapter_args.adapter_config) # add a new adapter model.add_adapter(task_name, config=adapter_config) # Enable adapter training model.train_adapter(task_name) ``` -------------------------------- ### ModelAdaptersConfig Source: https://docs.adapterhub.ml/classes/model_adapters_config.html Manages the setup and configuration of adapter modules in a pre-trained model. ```APIDOC ## class adapters.ModelAdaptersConfig ### Description Manages the setup and configuration of adapter modules in a pre-trained model. ### Methods #### add(adapter_name: str, config: Optional[Union[dict, str]] = None) Adds a new adapter of the name to the model config. Parameters: - **adapter_name** (str): The name of the adapter. - **config** (Optional[Union[str, dict]]): The adapter config. Defaults to None. #### add_fusion(adapter_names: List[str], config: Optional[Union[dict, str]] = None, fusion_name: Optional[str] = None) Adds a new AdapterFusion. Parameters: - **adapter_names** (List[str]): The names of the adapters to fuse. - **config** (Optional[Union[str, dict]]): AdapterFusion config. Defaults to None. - **fusion_name** (Optional[str]): The name of the AdapterFusion. If not specified, will default to comma-separated adapter names. #### common_config_value(adapter_names: list, attribute: str) Checks whether all adapters in a list share the same config setting for a given attribute and returns the shared value. Parameters: - **adapter_names** (list): The adapters to check. - **attribute** (str): The config attribute to check. #### get(adapter_name: str) -> Optional[dict] Gets the config dictionary for a given adapter. Parameters: - **adapter_name** (str): The name of the adapter. Returns: The adapter configuration. Return type: Optional[dict] #### get_fusion(fusion_name: Union[str, List[str]]) -> Tuple[Optional[dict], Optional[list]] Gets the config dictionary for a given AdapterFusion. Parameters: - **fusion_name** (Union[str, List[str]]): The name of the AdapterFusion or the adapters to fuse. Returns: The AdapterFusion configuration. Return type: Tuple[Optional[dict], Optional[list]] #### match(adapter_name: str, config_type: type, layer_idx: Optional[int] = None, location_key: Optional[str] = None) -> Optional[dict] Tries to match the given criteria to an existing adapter. Return the adapter config if a match is found, otherwise None. Parameters: - **adapter_name** (str): The name of the adapter. - **config_type** (type): The type of the adapter configuration to match. - **layer_idx** (Optional[int]): The layer index to match. - **location_key** (Optional[str]): The location key to match. Returns: The adapter config if a match is found, otherwise None. Return type: Optional[dict] ``` -------------------------------- ### Initialize Hugging Face model for adapters Source: https://docs.adapterhub.ml/_sources/transitioning.md.txt Prepare a Hugging Face model for use with adapters by calling `adapters.init()`. AdapterModel classes are already prepared. ```python from transformers import AutoModel import adapters model = AutoModel.from_pretrained("bert-base-uncased") adapters.init(model) # prepare model for use with adapters ``` -------------------------------- ### Get Module Dtype Source: https://docs.adapterhub.ml/classes/models/plbart.html Retrieves the dtype of the module, assuming all parameters have the same dtype. ```python model._dtype ``` -------------------------------- ### Get Extra Representation Source: https://docs.adapterhub.ml/classes/models/plbart.html Returns the extra representation of the module. Should be re-implemented for custom modules. ```python extra_repr() -> str ``` -------------------------------- ### ModelWithHeadsAdaptersMixin.load_adapter_setup Source: https://docs.adapterhub.ml/classes/model_mixins.html Loads an adapter setup from the local file system or a remote location. This method supports loading from HuggingFace Model Hub identifiers, local directories, or URLs, with options to set active, use safetensors, and provide custom weight loaders. ```APIDOC ## ModelWithHeadsAdaptersMixin.load_adapter_setup ### Description Loads an adapter setup from the local file system or a remote location. ### Method POST ### Endpoint /load_adapter_setup ### Parameters #### Path Parameters - **adapter_setup_name_or_path** (str) - Required - Can be either: * the identifier of a repository on the HuggingFace Model Hub. * a path to a directory containing adapter weights saved using model.save_adapter_setup() * a URL pointing to a zip folder containing a saved adapter module #### Query Parameters - **version** (str) - Optional - The version of the adapter to be loaded. - **set_active** (bool) - Optional - Set the loaded adapter setup to be the active one. By default (False), the adapter setup is loaded but not activated. - **use_safetensors** (bool) - Optional - If True, weights are loaded via safetensors if safetensors checkpoint is available. Otherwise, the regular torch save method is used. - **custom_weights_loaders** (List[WeightsLoader]) - Optional - Custom weights loaders. ### Returns - **Tuple[AdapterCompositionBlock, Any]** - The loaded adapter setup and the head setup if available. ``` -------------------------------- ### Get Dummy Inputs Source: https://docs.adapterhub.ml/classes/models/plbart.html Retrieves dummy inputs used for a forward pass in the network. ```python model._dummy_inputs ``` -------------------------------- ### num_parameters Source: https://docs.adapterhub.ml/classes/models/plbart.html Gets the number of parameters in the module, with options to count only trainable parameters or exclude embeddings. ```APIDOC ## num_parameters ### Description Get number of (optionally, trainable or non-embeddings) parameters in the module. ### Parameters #### Parameters - **only_trainable** (bool, optional, defaults to False) – Whether or not to return only the number of trainable parameters - **exclude_embeddings** (bool, optional, defaults to False) – Whether or not to return only the number of non-embeddings parameters ### Returns - The number of parameters. ### Return type int ``` -------------------------------- ### Initialize EncoderDecoderModel and Convert to Adapter Model Source: https://docs.adapterhub.ml/classes/models/encoderdecoder.html Create an EncoderDecoderModel instance and then convert it to an adapter model using `adapters.init()`. This is the recommended approach when direct AutoModel support is unavailable. ```python from transformers import EncoderDecoderModel import adapters # 1. Create an EncoderDecoderModel instance model = EncoderDecoderModel.from_encoder_decoder_pretrained("bert-base-uncased", "bert-base-uncased") # 2. Convert this model to an adapter model adapters.init(model) ``` -------------------------------- ### Get Modules Iterator Source: https://docs.adapterhub.ml/classes/models/plbart.html Returns an iterator over all modules in the network, with an option to remove duplicate instances. ```python modules(_remove_duplicate : bool = True_) → Iterator[Module] Return an iterator over all modules in the network. Parameters **remove_duplicate** – whether to remove the duplicated module instances in the result or not. Yields _Module_ – a module in the network ``` -------------------------------- ### BertAdapterModel Source: https://docs.adapterhub.ml/classes/models/bert.html Initializes a BertAdapterModel. This class allows for loading, managing, and merging adapter setups for BERT models. ```APIDOC ## BertAdapterModel ### Description Initializes a BertAdapterModel. This class allows for loading, managing, and merging adapter setups for BERT models. ### Parameters * **adapter_setup_name_or_path** (str) - Required - Can be either the identifier of a repository on the HuggingFace Model Hub, a path to a directory containing adapter weights saved using model.save_adapter_setup(), or a URL pointing to a zip folder containing a saved adapter module. * **version** (str, optional) - The version of the adapter to be loaded. * **set_active** (bool, optional) - Set the loaded adapter setup to be the active one. By default (False), the adapter setup is loaded but not activated. * **use_safetensors** (bool, optional) - If True, weights are loaded via safetensors if safetensors checkpoint is available. Otherwise, the regular torch save method is used. ### Returns - Tuple[AdapterCompositionBlock, Any] - The loaded adapter setup and the head setup if available. ``` -------------------------------- ### Dynamic Adapter Activation with AdapterSetup Source: https://docs.adapterhub.ml/_sources/adapter_composition.md.txt Use the `AdapterSetup` context manager to temporarily activate adapters without altering the model's global state. This is useful for testing or specific forward passes. ```python from adapters import AdapterSetup model = ... model.add_adapter("adapter_name") with AdapterSetup("adapter_name"): # will use the adapter named "adapter_name" in the forward pass outputs = model(**inputs) ``` -------------------------------- ### Initialize T5Tokenizer and T5Model Source: https://docs.adapterhub.ml/classes/models/t5.html Loads the tokenizer and model for the 'google-t5/t5-small' configuration. Ensure you have the transformers library installed. ```python from transformers import AutoTokenizer, T5Model tokenizer = AutoTokenizer.from_pretrained("google-t5/t5-small") model = T5Model.from_pretrained("google-t5/t5-small") ``` -------------------------------- ### Initialize BeitAdapterModel with Adapters Source: https://docs.adapterhub.ml/classes/models/beit.html This method initializes adapter modules and fusion modules from the model config. It's crucial for setting up adapter layers within the Beit model. ```python model.init_adapters(model_config, adapters_config) ``` -------------------------------- ### ConfigUnion Equivalent Source: https://docs.adapterhub.ml/overview.html This snippet shows the equivalent representation of the configuration string using the ConfigUnion class, which allows combining multiple adapter configurations. ```python config = ConfigUnion( PrefixTuningConfig(bottleneck_size=800), ParBnConfig(), ) ``` -------------------------------- ### Get Input Embeddings Source: https://docs.adapterhub.ml/classes/models/plbart.html Returns the model's input embeddings module, which maps vocabulary to hidden states. ```python get_input_embeddings() -> Module: """ Returns the model’s input embeddings. Returns A torch module mapping vocabulary to hidden states. Return type nn.Module """ pass ``` -------------------------------- ### Combine LoRA, Prefix Tuning, and SeqBnConfig with gating Source: https://docs.adapterhub.ml/_sources/method_combinations.md.txt Create a UniPELT-like configuration by combining `LoRAConfig`, `PrefixTuningConfig`, and `SeqBnConfig` with enabled gating mechanisms using `ConfigUnion`. ```python from adapters import ConfigUnion, LoRAConfig, PrefixTuningConfig, SeqBnConfig config = ConfigUnion( LoRAConfig(r=8, alpha=2, use_gating=True), PrefixTuningConfig(prefix_length=10, use_gating=True), SeqBnConfig(reduction_factor=16, use_gating=True), ) model.add_adapter("unipelt", config=config) ``` -------------------------------- ### active_adapters Source: https://docs.adapterhub.ml/classes/models/deberta_v2.html Gets the current active adapters of the model. Returns a list of all active adapters in case of multi-adapter inference. ```APIDOC ## _property _active_adapters _property _active_adapters _: AdapterCompositionBlock_ ### Description If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT official documentation: https://huggingface.co/docs/peft Gets the current active adapters of the model. In case of multi-adapter inference (combining multiple adapters for inference) returns the list of all active adapters so that users can deal with them accordingly. For previous PEFT versions (that does not support multi-adapter inference), module.active_adapter will return a single string. ``` -------------------------------- ### ConfigUnion Source: https://docs.adapterhub.ml/classes/adapter_config.html Composes multiple adaptation method configurations into one. This class can be used to define complex adaptation method setups. ```APIDOC ## ConfigUnion ### Description Composes multiple adaptation method configurations into one. This class can be used to define complex adaptation method setups. ### Class Methods * `_from_dict(config)`: Creates a config class from a Python dict. * `_load(config: Union[dict, str], download_kwargs =None, **kwargs)`: Loads a given adapter configuration specifier into a full AdapterConfig instance. ### Methods * `replace(**changes)`: Returns a new instance of the config class with the specified changes applied. * `to_dict()`: Converts the config class to a Python dict. * `_static _validate(configs)`: Performs simple validations of a list of configurations to check whether they can be combined to a common setup. ### Parameters for _validate * **configs** (List[AdapterConfig]): list of configs to check. ### Raises for _validate * **TypeError**: One of the configurations has a wrong type. * **ValueError**: At least two given configurations conflict. ``` -------------------------------- ### BeitAdapterModel Forward Pass Example Source: https://docs.adapterhub.ml/classes/models/beit.html This snippet demonstrates a basic forward pass through the BeitAdapterModel. It's a placeholder and would typically involve passing input data to the model. ```python model = BeitAdapterModel.from_pretrained("microsoft/beit-base-patch16-224-pt224") inputs = { "pixel_values": torch.randn(1, 3, 224, 224) } outputs = model(**inputs) pooled_output = outputs.pooler_output ``` -------------------------------- ### Instantiate AutoAdapterModel Source: https://docs.adapterhub.ml/_sources/training.md.txt Replace AutoModelForSequenceClassification with AutoAdapterModel to leverage adapter functionalities. This example also shows how to add a classification head. ```python model = AutoAdapterModel.from_pretrained( model_args.model_name_or_path, config=config, ) model.add_classification_head(data_args.task_name, num_labels=num_labels) ``` -------------------------------- ### Initialize Adapters Source: https://docs.adapterhub.ml/classes/models/plbart.html Initializes adapter modules and fusion modules using the provided model and adapter configurations. ```python init_adapters(_model_config_ , _adapters_config_) ``` -------------------------------- ### Count Model Parameters Source: https://docs.adapterhub.ml/classes/models/plbart.html Get the total number of parameters in a module. Options are available to count only trainable parameters or exclude embeddings. ```python num_parameters() ``` ```python num_parameters(only_trainable=True) ``` ```python num_parameters(exclude_embeddings=True) ```