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

Allow source=None to search all source in load_adapter() #309

Merged
merged 1 commit into from
Mar 23, 2022
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
6 changes: 3 additions & 3 deletions src/transformers/adapters/model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def load_adapter(
version: str = None,
model_name: str = None,
load_as: str = None,
source: str = "ah",
source: str = None,
custom_weights_loaders: Optional[List[WeightsLoader]] = None,
leave_out: Optional[List[int]] = None,
id2label=None,
Expand All @@ -471,7 +471,7 @@ def load_adapter(

- "ah" (default): search on AdapterHub.
- "hf": search on HuggingFace model hub.
- None: only search on local file system
- None: 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 activated.

Expand Down Expand Up @@ -876,7 +876,7 @@ def load_adapter(
version: str = None,
model_name: str = None,
load_as: str = None,
source: str = "ah",
source: str = None,
with_head: bool = True,
custom_weights_loaders: Optional[List[WeightsLoader]] = None,
leave_out: Optional[List[int]] = None,
Expand Down
25 changes: 24 additions & 1 deletion src/transformers/adapters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def resolve_adapter_path(
model_name: str = None,
adapter_config: Union[dict, str] = None,
version: str = None,
source: str = "ah",
source: str = None,
**kwargs
) -> str:
"""
Expand All @@ -438,6 +438,11 @@ def resolve_adapter_path(
model_name (str, optional): The identifier of the pre-trained model for which to load an adapter.
adapter_config (Union[dict, str], optional): The configuration of the adapter to be loaded.
version (str, optional): The version of the adapter to be loaded. Defaults to None.
source (str, optional): Identifier of the source(s) from where to get adapters. Can be either:

- "ah": search on AdapterHub.ml.
- "hf": search on HuggingFace model hub (huggingface.co).
- None (default): search on all sources

Returns:
str: The local path from where the adapter module can be loaded.
Expand Down Expand Up @@ -466,6 +471,24 @@ def resolve_adapter_path(
)
elif source == "hf":
return pull_from_hf_model_hub(adapter_name_or_path, version=version, **kwargs)
elif source is None:
try:
logger.info("Attempting to load adapter from source 'ah'...")
return pull_from_hub(
adapter_name_or_path, model_name, adapter_config=adapter_config, version=version, **kwargs
)
except EnvironmentError as ex:
logger.info(ex)
logger.info("Attempting to load adapter from source 'hf'...")
try:
return pull_from_hf_model_hub(adapter_name_or_path, version=version, **kwargs)
except Exception as ex:
hSterz marked this conversation as resolved.
Show resolved Hide resolved
logger.info(ex)
raise EnvironmentError(
"Unable to load adapter {} from any source. Please check the name of the adapter or the source.".format(
adapter_name_or_path
)
)
else:
raise ValueError("Unable to identify {} as a valid module location.".format(adapter_name_or_path))

Expand Down