Skip to content

vllm.transformers_utils.repo_utils

Utilities for model repo interaction.

Functions:

  • get_hf_file_bytes –

    Get file contents from HuggingFace repository as bytes.

  • get_hf_file_to_dict –

    Downloads a file from the Hugging Face Hub and returns

  • get_non_weight_snapshot_path –

    Download all non-weight files of a Hub repo and return the local snapshot.

  • get_subfolder_path –

    Return the local path of subfolder in a local or Hub checkpoint,

  • hf_api –

    Return a shared HfApi instance tagged with vLLM's library info.

  • hf_fs –

    Return a fresh HfFileSystem tagged with vLLM's library info.

  • resolve_revision –

    Best-effort attempt to resolve a Hub revision to a commit hash.

  • try_get_local_file –

    Try to get a local file from the HuggingFace repository.

  • with_retry –

    Call func, retrying transient failures.

_try_download_from_hf_hub(model, file_name, revision)

Try to download a file from HuggingFace Hub.

Returns the local path on success, None on failure. Skips download if model is a local directory.

get_hf_file_bytes(file_name, model, revision='main')

Get file contents from HuggingFace repository as bytes.

get_hf_file_to_dict(file_name, model, revision='main')

Downloads a file from the Hugging Face Hub and returns its contents as a dictionary.

Parameters:

  • file_name

    (str) –

    The name of the file to download.

  • model

    (str) –

    The name of the model on the Hugging Face Hub.

  • revision

    (str, default: 'main' ) –

    The specific version of the model.

Returns: - config_dict (dict): A dictionary containing the contents of the downloaded file.

get_non_weight_snapshot_path(repo_id, revision=None, cache_dir=None, token=None)

Download all non-weight files of a Hub repo and return the local snapshot.

Loading tokenizers and processors from a local directory stops Transformers from making a Hub request per file (including the additional_chat_templates listing). Returns repo_id unchanged if it is local or cannot be fetched, so Transformers handles it as before. Failures are not cached, so they are retried.

get_subfolder_path(model, subfolder, revision=None, cache_dir=None)

Return the local path of subfolder in a local or Hub checkpoint, downloading only that subfolder from the Hub.

hf_api()

Return a shared HfApi instance tagged with vLLM's library info.

hf_fs()

Return a fresh HfFileSystem tagged with vLLM's library info.

resolve_revision(repo_id, revision=None, token=None)

Best-effort attempt to resolve a Hub revision to a commit hash.

Resolving the commit hash once prevents repeated HTTP calls downstream and avoids races if the branch moves while the model is loading.

HfApi.resolve_revision returns a ResolvedRevision: a str equal to the requested revision that also carries the commit hash, so downstream error messages stay readable and offline loads reuse the cached refs/ entry.

A commit hash only means something for the repo it was resolved against, so a repo that is not repo_id needs its own call. Passing a revision resolved for another repo back in is safe: the revision initially requested is resolved again, against repo_id this time.

Returns:

  • str | None –

    The resolved revision, or revision unchanged if it cannot be resolved

  • str | None –

    (local path, ModelScope, or any Hub error).

try_get_local_file(model, file_name, revision='main')

Try to get a local file from the HuggingFace repository.

The possible return values are:

  • A Path object if the local file is found
  • The huggingface_hub._CACHED_NO_EXIST sentinel if the file is known to not exist
  • None if the file is not found and we cannot determine if it exists or not

Callers of this method should handle the _CACHED_NO_EXIST sentinel appropriately. Checking if the return value is not None is not sufficient because it does not distinguish between the file not existing and the file not being found.

with_retry(func, log_msg, max_retries=2, retry_delay=2, fatal_errors=())

Call func, retrying transient failures.

Parameters:

  • func

    (Callable[[], _R]) –

    The call to make.

  • log_msg

    (str) –

    Prefix for the message logged when a call fails.

  • max_retries

    (int, default: 2 ) –

    How many times to call func before giving up.

  • retry_delay

    (int, default: 2 ) –

    Seconds to wait after the first failure, doubled each time.

  • fatal_errors

    (tuple[type[Exception], ...], default: () ) –

    Exceptions that are a definitive answer rather than a transient failure. These are raised immediately, without logging.