Adapter Utilities

A collection of utility methods mainly related to searching and loading adapter modules from Adapter-Hub.

class transformers.adapters.utils.AdapterInfo(source: str, adapter_id: str, model_name: Optional[str] = None, task: Optional[str] = None, subtask: Optional[str] = None, username: Optional[str] = None, adapter_config: Optional[dict] = None, sha1_checksum: Optional[str] = None)

Holds information about an adapter publicly available on AdapterHub or huggingface.co. Returned by list_adapters().

Parameters
  • source (str) – The source repository of this adapter. Can be either “ah” (AdapterHub) or “hf” (huggingface.co).

  • adapter_id (str) – The unique identifier of this adapter.

  • model_name (str, optional) – The identifier of the model this adapter was trained for.

  • task (str, optional) – The task this adapter was trained for.

  • subtask (str, optional) – The subtask or dataset this adapter was trained on.

  • username (str, optional) – The username of author(s) of this adapter.

  • adapter_config (dict, optional) – The configuration dictionary of this adapter.

class transformers.adapters.utils.AdapterType(value)

Models all currently available model adapter types.

transformers.adapters.utils.get_adapter_config_hash(config, length=16)

Calculates the hash of a given adapter configuration which is used to identify this configuration.

Returns

The resulting hash of the given config dict.

Return type

str

transformers.adapters.utils.get_adapter_info(adapter_id: str, source: str = 'ah') → Optional[transformers.adapters.utils.AdapterInfo]

Retrieves information about a specific adapter.

Parameters
  • adapter_id (str) – The identifier of the adapter to retrieve.

  • 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).

Returns

The adapter information or None if the adapter was not found.

Return type

AdapterInfo

transformers.adapters.utils.get_from_cache(url: str, cache_dir=None, force_download=False, proxies=None, etag_timeout=10, resume_download=False, user_agent: Optional[Union[Dict, str]] = None, use_auth_token: Optional[Union[bool, str]] = None, local_files_only=False) → Optional[str]

Given a URL, look for the corresponding file in the local cache. If it’s not there, download it. Then return the path to the cached file. Return:

Local path (string) of file or if networking is off, last version of file cached on disk.

Raises

In case of non-recoverable file (non-existent or inaccessible url + no cache on disk)

transformers.adapters.utils.list_adapters(source: str = None, model_name: str = None) → List[transformers.adapters.utils.AdapterInfo]

Retrieves a list of all publicly available adapters on AdapterHub.ml or on huggingface.co.

Parameters
  • 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

  • model_name (str, optional) – If specified, only returns adapters trained for the model with this identifier.

transformers.adapters.utils.parse_adapter_config_string(config_string: str) → List[Tuple[str, dict]]

Parses an adapter configuration string into a list of tuples. Each tuple constists of an adapter config identifier and dictionary.

transformers.adapters.utils.pull_from_hub(specifier: str, model_name: str, adapter_config: Optional[Union[dict, str]] = None, version: str = None, strict: bool = False, **kwargs) → str

Downloads a pre-trained adapter module from Adapter-Hub

Parameters
  • specifier (str) – A string specifying the adapter to be loaded.

  • model_name (str) – 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.

  • strict (bool, optional) – If set to True, only allow adapters exactly matching the given config to be loaded. Defaults to False.

Returns

The local path to which the adapter has been downloaded.

Return type

str

transformers.adapters.utils.resolve_adapter_config(config: Union[dict, str], local_map=None, try_loading_from_hub=True, **kwargs) → dict

Resolves a given adapter configuration specifier to a full configuration dictionary.

Parameters

config (Union[dict, str]) –

The configuration to resolve. Can be either:

  • a dictionary: returned without further action

  • an identifier string available in local_map

  • the path to a file containing a full adapter configuration

  • an identifier string available in Adapter-Hub

Returns

The resolved adapter configuration dictionary.

Return type

dict

transformers.adapters.utils.resolve_adapter_path(adapter_name_or_path, model_name: str = None, adapter_config: Union[dict, str] = None, version: str = None, source: str = None, **kwargs) → str

Resolves the path to a pre-trained adapter module. Note: If attempting to resolve an adapter from the Hub, adapter_config and model_name must be present.

Parameters
  • adapter_name_or_path (str) –

    Can be either:

    • the path to a folder in the file system containing the adapter configuration and weights

    • an url pointing to a zip folder containing the adapter configuration and weights

    • a specifier matching a pre-trained adapter uploaded to Adapter-Hub

  • 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

The local path from where the adapter module can be loaded.

Return type

str