xnn.common.models.hub.loader.fetch_model#

xnn.common.models.hub.loader.fetch_model(source, *, cache_dir=None, filename=None, head=None, format=None, force_download=False, local_files_only=False, quiet=False)[source]#

Return a local copy of a model, downloading and converting it if needed.

Sources are tried in this order:

  1. an existing local path (a model directory or a checkpoint file), used in place;

  2. a registered name (list_models()), fetched from the DOI or URL in its card into <cache_dir>/<name>/;

  3. a Zenodo DOI or record link, into <cache_dir>/zenodo.<id>/<file>/;

  4. a plain http(s) URL of a checkpoint or archive, into <cache_dir>/url/<file>-<hash>/;

  5. the key of a directory already in the cache (as list_models() shows it), e.g. one copied over from another machine.

Whatever is fetched is stored as a portable model directory (see save_pretrained()), so the next call needs no network and no converter package.

Parameters:
  • source (str or pathlib.Path) – What to load.

  • cache_dir (str or pathlib.Path, optional) – Cache directory. Defaults to default_model_cache_dir().

  • filename (str, optional) – File to take from a Zenodo record that holds several models.

  • head (str, optional) – Head to keep from a multi-head model.

  • format (str, optional) – Format of the published file when its suffix does not tell ("xnn", "mace-torch").

  • force_download (bool, optional) – Download and convert again even when cached.

  • local_files_only (bool, optional) – Never use the network (also set by XNN_OFFLINE=1); fail when the model is not cached.

  • quiet (bool, optional) – Hide download progress bars.

Returns:

A model directory, or the local file that was passed in.

Return type:

pathlib.Path

Raises:
  • FileNotFoundError – If the source is not found, or not cached while offline.

  • ValueError – For a DOI that is not on Zenodo, or an ambiguous record or head.