Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove loading adapters from 'ah' & source parameter #724

Merged
merged 4 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/classes/models/plbart.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PLBART
=====
======

The PLBART model was proposed in [Unified Pre-training for Program Understanding and Generation](https://arxiv.org/abs/2103.06333) by Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang.
This is a BART-like model which can be used to perform code-summarization, code-generation, and code-translation tasks. The pre-trained model `plbart-base` has been trained using multilingual denoising task
Expand Down
12 changes: 4 additions & 8 deletions docs/huggingface_hub.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,21 @@ For more options and information, e.g. for managing models via the CLI and Git,
model.push_adapter_to_hub(
"my-awesome-adapter",
"awesome_adapter",
adapterhub_tag="sentiment/imdb",
datasets_tag="imdb"
)
```
This will create a repository `my-awesome-adapter` under your username, generate a default adapter card as `README.md` and upload the adapter named `awesome_adapter` together with the adapter card to the new repository.
`adapterhub_tag` and `datasets_tag` provide additional information for categorization.
`datasets_tag` provides additional information for categorization.

```{eval-rst}
.. important::
All adapters uploaded to Hugging Face's Model Hub are automatically also listed on AdapterHub.ml. Thus, for better categorization, either ``adapterhub_tag`` or ``datasets_tag`` is required when uploading a new adapter to the Model Hub.

- ``adapterhub_tag`` specifies the AdapterHub categorization of the adapter in the format ``<task>/<subtask>`` according to the tasks and subtasks shown on https://adapterhub.ml/explore. For more, see `Add a new task or subtask <https://docs.adapterhub.ml/contributing.html#add-a-new-task-or-subtask>`_.
- ``datasets_tag`` specifies the dataset the adapter was trained on as an identifier from `Hugging Face Datasets <https://huggingface.co/datasets>`_.
.. note::
All adapters uploaded to Hugging Face's Model Hub are automatically also listed on AdapterHub.ml. Thus, for better categorization, ``datasets_tag`` is helpful when uploading a new adapter to the Model Hub. ``datasets_tag`` specifies the dataset the adapter was trained on as an identifier from `Hugging Face Datasets <https://huggingface.co/datasets>`_.
```

Voilà! Your first adapter is on the Hugging Face Model Hub.
Anyone can now run:
```
model.load_adapter("<your_username>/my-awesome-adapter", source="hf")
model.load_adapter("<your_username>/my-awesome-adapter")
```

To update your adapter, simply run `push_adapter_to_hub()` with the same repository name again. This will push a new commit to the existing repository.
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Currently, we support the PyTorch versions of all models as listed on the `Model
classes/models/gpt2
classes/models/gptj
classes/models/llama
classes/models/mistral
classes/models/mbart
classes/models/mt5
classes/models/plbart
Expand Down
36 changes: 12 additions & 24 deletions docs/loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ E.g., we can use it to retrieve information for all adapters trained for a speci
```python
from adapters import list_adapters

# source can be "ah" (archived Hub repo), "hf" (huggingface.co) or None (for both, default)
adapter_infos = list_adapters(source="hf", model_name="bert-base-uncased")
adapter_infos = list_adapters(model_name="bert-base-uncased")

for adapter_info in adapter_infos:
print("Id:", adapter_info.adapter_id)
Expand All @@ -29,7 +28,7 @@ for adapter_info in adapter_infos:
In case the adapter ID is known, information for a single adapter can also be retrieved via [`get_adapter_info()`](adapters.utils.get_adapter_info):

```python
adapter_info = get_adapter_info("@ukp/bert-base-uncased_sentiment_sst-2_pfeiffer", source="ah")
adapter_info = get_adapter_info("AdapterHub/roberta-base-pf-imdb")

print("Id:", adapter_info.adapter_id)
print("Model name:", adapter_info.model_name)
Expand Down Expand Up @@ -62,14 +61,14 @@ model.set_active_adapters(adapter_name)

As the second example, let's have a look at how to load an adapter based on the [`AdapterInfo`](adapters.utils.AdapterInfo) returned by the [`list_adapters()`](adapters.utils.list_adapters) method from [above](#finding-pre-trained-adapters):
```python
from adapters import AutoAdapterModel, list_available_adapters
from adapters import AutoAdapterModel, list_adapters

adapter_infos = list_available_adapters(source="ah")
adapter_infos = list_adapters()
# Take the first adapter info as an example
adapter_info = adapter_infos[0]

model = AutoAdapterModel.from_pretrained(adapter_info.model_name)
model.load_adapter(adapter_info.adapter_id, source=adapter_info.source)
model.load_adapter(adapter_info.adapter_id)
```

### Advanced usage of `load_adapter()`
Expand All @@ -78,31 +77,20 @@ To examine what's happening underneath in a bit more detail, let's first write o

```python
model.load_adapter(
'sst-2',
config='pfeiffer',
model_name='bert-base-uncased',
version=1,
load_as='sst',
source='ah'
"AdapterHub/roberta-base-pf-imdb",
version="main",
load_as="sentiment_adapter",
set_active=True,
)
```

We will go through the different arguments and their meaning one by one:

- The first argument passed to the method specifies the name of the adapter we want to load from Adapter-Hub. The library will search for an available adapter module with this name that matches the model architecture as well as the adapter type and configuration we requested. As the identifier `sst-2` resolves to a unique entry in the Hub, the corresponding adapter can be successfully loaded based on this information. To get an overview of all available adapter identifiers, please refer to [the Adapter-Hub website](https://adapterhub.ml/explore).
- The first argument passed to the method specifies the name or path from where to load the adapter. This can be the name of a repository on the [HuggingFace Model Hub](https://huggingface.co/models), a local path or a URL. To get an overview of all available adapters on the Hub, please refer to [the Adapter-Hub website](https://adapterhub.ml/explore).

- The `config` argument defines the adapter architecture the loaded adapter should have.
The value of this parameter can be either a string identifier for one of the predefined architectures, the identifier of an architecture available in the Hub or a dictionary representing a full adapter configuration.
Based on this information, the library will only search for pre-trained adapter modules having the same configuration.

- Adapter modules trained on different pre-trained language models in general can not be used interchangeably.
Therefore, we need to make sure to load an adapter matching the language model we are using.
If possible, the library will infer the name of the pre-trained model automatically (e.g. when we use `from_pretrained('identifier')` to load a model from Hugging Face). However, if this is not the case, we must specify the name of the host model in the `model_name` parameter.

- There could be multiple versions of the same adapter available. To load a specific version, use the `version` parameter.
- There could be multiple versions of the same adapter available as revisions in a Model Hub repository. To load a specific revision, use the `version` parameter.

- By default, the `load_adapter()` method will add the loaded adapter using the identifier string given as the first argument.
To load the adapter using a custom name, we can use the `load_as` parameter.

- Finally the `source` parameter provides the possibility to load adapters from alternative adapter repositories.
Besides the default value `ah`, referring to AdapterHub, it's also possible to pass `hf` to [load adapters from Hugging Face's Model Hub](huggingface_hub.md).
- Finally, `set_active` will directly activate the loaded adapter for usage in each model forward pass. Otherwise, you have to manually activate the adapter via `set_active_adapters()`.
2 changes: 1 addition & 1 deletion src/adapters/configuration/adapter_fusion_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def load(cls, config: Union[dict, str], **kwargs):
dict: The resolved adapter fusion configuration dictionary.
"""
# currently storing AdapterFusion weights on AdapterHub is not supported.
config_dict = resolve_adapter_config(config, local_map=ADAPTERFUSION_CONFIG_MAP, try_loading_from_hub=False)
config_dict = resolve_adapter_config(config, local_map=ADAPTERFUSION_CONFIG_MAP)
# convert back to dict to allow attr overrides
if isinstance(config_dict, AdapterFusionConfig):
config_dict = config_dict.to_dict()
Expand Down
50 changes: 4 additions & 46 deletions src/adapters/hub_mixin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import os
import warnings
from typing import List, Optional, Union

from transformers.utils.generic import working_or_temp_dir
Expand Down Expand Up @@ -36,7 +35,7 @@
from adapters import AutoAdapterModel

model = AutoAdapterModel.from_pretrained("{model_name}")
adapter_name = model.load_adapter("{adapter_repo_name}", source="hf", set_active=True)
adapter_name = model.load_adapter("{adapter_repo_name}", set_active=True)
```

## Architecture & Training
Expand All @@ -62,7 +61,6 @@ def _save_adapter_card(
save_directory: str,
adapter_name: str,
adapter_repo_name: str,
adapterhub_tag: Optional[str] = None,
datasets_tag: Optional[str] = None,
tags: Optional[List[str]] = None,
language: Optional[str] = None,
Expand All @@ -75,15 +73,9 @@ def _save_adapter_card(
datasets = set()
# Dataset/ Task info
dataset_name = None
if adapterhub_tag is None and datasets_tag is None:
raise ValueError("Either adapterhub_tag or datasets_tag must be specified.")
if datasets_tag is not None:
dataset_name = f"[{datasets_tag}](https://huggingface.co/datasets/{datasets_tag}/)"
datasets.add(datasets_tag)
if adapterhub_tag is not None:
# adapterhub_tag overwrites datasets_tag
dataset_name = f"[{adapterhub_tag}](https://adapterhub.ml/explore/{adapterhub_tag}/)"
all_tags.add(f"adapterhub:{adapterhub_tag}")

all_tags.add(self.config.model_type)
if tags is not None:
Expand Down Expand Up @@ -123,10 +115,8 @@ def _save_adapter_card(

def push_adapter_to_hub(
self,
repo_name: str,
repo_id: str,
adapter_name: str,
organization: Optional[str] = None,
adapterhub_tag: Optional[str] = None,
datasets_tag: Optional[str] = None,
local_path: Optional[str] = None,
commit_message: Optional[str] = None,
Expand All @@ -137,21 +127,15 @@ def push_adapter_to_hub(
revision: str = None,
commit_description: str = None,
adapter_card_kwargs: Optional[dict] = None,
**deprecated_kwargs,
):
"""Upload an adapter to HuggingFace's Model Hub.

Args:
repo_name (str): The name of the repository on the model hub to upload to.
repo_id (str): The name of the repository on the model hub to upload to.
adapter_name (str): The name of the adapter to be uploaded.
organization (str, optional): Organization in which to push the adapter
(you must be a member of this organization). Defaults to None.
adapterhub_tag (str, optional):
Tag of the format `<task>/<subtask>` for categorization on https://adapterhub.ml/explore/. See
https://docs.adapterhub.ml/contributing.html#add-a-new-task-or-subtask for more. If not specified,
`datasets_tag` must be given in case a new adapter card is generated. Defaults to None.
datasets_tag (str, optional): Dataset identifier from https://huggingface.co/datasets.
If not specified, `adapterhub_tag` must be given in case a new adapter card is generated. Defaults to
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.
Expand All @@ -176,31 +160,6 @@ def push_adapter_to_hub(
Returns:
str: The url of the adapter repository on the model hub.
"""
use_auth_token = deprecated_kwargs.pop("use_auth_token", None)
if use_auth_token is not None:
warnings.warn(
"The `use_auth_token` argument is deprecated and will be removed in future versions of Adapters."
" Please use `token` instead.",
FutureWarning,
)
if token is not None:
raise ValueError(
"`token` and `use_auth_token` are both specified. Please set only the argument `token`."
)
token = use_auth_token

if organization is not None and not repo_name.startswith(organization):
warnings.warn(
"The `organization` argument is deprecated and will be removed in future versions of"
" Adapters. Set your organization directly in the `repo_id` passed instead"
" (`repo_id={organization}/{model_id}`)."
)
if "/" in repo_name:
repo_name = repo_name.split("/")[-1]
repo_id = f"{organization}/{repo_name}"
else:
repo_id = repo_name

use_temp_dir = not os.path.isdir(local_path) if local_path else True

# Create repo or get retrieve an existing repo
Expand All @@ -218,7 +177,6 @@ def push_adapter_to_hub(
work_dir,
adapter_name,
repo_id,
adapterhub_tag=adapterhub_tag,
datasets_tag=datasets_tag,
**adapter_card_kwargs,
)
Expand Down
11 changes: 9 additions & 2 deletions src/adapters/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,16 +518,23 @@ def load(
- the identifier of a pre-trained task adapter to be loaded from Adapter Hub
- a path to a directory containing adapter weights saved using `model.saved_adapter()`
- a URL pointing to a zip folder containing a saved adapter module
config (str, optional): The requested configuration of the adapter.
config (str, optional): Deprecated.
version (str, optional): The version of the adapter to be loaded.
model_name (str, optional): The string identifier of the pre-trained model.
model_name (str, optional): Deprecated.
load_as (str, optional): Load the adapter using this name. By default, the name with which the adapter was
saved will be used.

Returns:
Tuple[str, str]: A tuple consisting of the local file system directory from which the weights where loaded
and the name of the loaded weights.
"""
# Warn about deprecated arguments
if config is not None or model_name is not None:
logger.warning(
"The 'config' and 'model_name' arguments are specific to the now unsupported legacy Hub repo and will"
" be removed."
"Please switch to only providing the HF Model Hub identifier.",
)
requested_config = AdapterConfig.load(config) if config else None
# Resolve the weights to be loaded based on the given identifier and the current adapter config
model_name = self.model.model_name or model_name
Expand Down
17 changes: 2 additions & 15 deletions src/adapters/model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,6 @@ def load_adapter(
version: str = None,
model_name: str = None,
load_as: str = None,
source: str = None,
custom_weights_loaders: Optional[List[WeightsLoader]] = None,
leave_out: Optional[List[int]] = None,
id2label=None,
Expand All @@ -788,20 +787,11 @@ def load_adapter(
- the identifier of a pre-trained task adapter to be loaded from Adapter Hub
- a path to a directory containing adapter weights saved using `model.saved_adapter()`
- a URL pointing to a zip folder containing a saved adapter module
config (dict or str, optional): The requested configuration of the adapter.
If not specified, will be either: - the default adapter config for the requested adapter if specified -
the global default adapter config
config (dict or str, optional): Deprecated.
version (str, optional): The version of the adapter to be loaded.
model_name (str, optional): The string identifier of the pre-trained model.
model_name (str, optional): Deprecated.
load_as (str, optional): Load the adapter using this name. By default, the name with which the adapter was
saved will be used.
source (str, optional): Identifier of the source(s) from where to load the adapter. Can be:

- "ah": search on AdapterHub Hub repo.
Note: the Hub repo has been archived and all adapters have been moved to HuggingFace Model Hub.
Loading from this source is deprecated.
- "hf": search on HuggingFace Model Hub.
- None (default): search on all sources
leave_out: Dynamically drop adapter modules in the specified Transformer layers when loading the adapter.
set_active (bool, optional):
Set the loaded adapter to be the active one. By default (False), the adapter is loaded but not
Expand All @@ -818,7 +808,6 @@ def load_adapter(
version,
model_name,
load_as,
source=source,
leave_out=leave_out,
set_active=set_active,
**kwargs,
Expand Down Expand Up @@ -1538,7 +1527,6 @@ def load_adapter(
version: str = None,
model_name: str = None,
load_as: str = None,
source: str = None,
with_head: bool = True,
custom_weights_loaders: Optional[List[WeightsLoader]] = None,
leave_out: Optional[List[int]] = None,
Expand Down Expand Up @@ -1568,7 +1556,6 @@ def load_adapter(
version=version,
model_name=model_name,
load_as=load_as,
source=source,
custom_weights_loaders=custom_weights_loaders,
leave_out=leave_out,
id2label=id2label,
Expand Down
Loading
Loading