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

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

  • try_get_local_file

    Try to get a local file from the HuggingFace repository.

_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.

Source code in vllm/transformers_utils/repo_utils.py
def _try_download_from_hf_hub(
    model: str | Path, file_name: str, revision: str | None
) -> Path | None:
    """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.
    """
    if Path(model).is_dir():
        return None
    try:
        return Path(
            hf_api().hf_hub_download(
                model,
                file_name,
                revision=revision,
            )
        )
    except huggingface_hub.errors.OfflineModeIsEnabled:
        return None
    except (
        RepositoryNotFoundError,
        RevisionNotFoundError,
        EntryNotFoundError,
        LocalEntryNotFoundError,
    ) as e:
        logger.debug("File or repository not found in hf_hub_download: %s", e)
        return None
    except HfHubHTTPError as e:
        logger.warning(
            "Cannot connect to Hugging Face Hub. Skipping file download for '%s':",
            file_name,
            exc_info=e,
        )
        return None

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

Get file contents from HuggingFace repository as bytes.

Source code in vllm/transformers_utils/repo_utils.py
def get_hf_file_bytes(
    file_name: str, model: str | Path, revision: str | None = "main"
) -> bytes | None:
    """Get file contents from HuggingFace repository as bytes."""
    file_path = try_get_local_file(model=model, file_name=file_name, revision=revision)

    if file_path is None:
        file_path = _try_download_from_hf_hub(model, file_name, revision)

    if isinstance(file_path, Path) and file_path.is_file():
        with open(file_path, "rb") as file:
            return file.read()

    return None

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): The specific version of the model.

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

Source code in vllm/transformers_utils/repo_utils.py
def get_hf_file_to_dict(
    file_name: str, model: str | Path, revision: str | None = "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): The specific version of the model.

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

    file_path = try_get_local_file(model=model, file_name=file_name, revision=revision)

    if file_path is None:
        file_path = _try_download_from_hf_hub(model, file_name, revision)

    if isinstance(file_path, Path) and file_path.is_file():
        with open(file_path) as file:
            return json.load(file)

    return None

hf_api()

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

Source code in vllm/transformers_utils/repo_utils.py
def hf_api() -> HfApi:
    """Return a shared HfApi instance tagged with vLLM's library info."""
    global _hf_api
    if _hf_api is None:
        _hf_api = HfApi(
            library_name="vllm",
            library_version=VLLM_VERSION,
        )
    return _hf_api

hf_fs()

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

Source code in vllm/transformers_utils/repo_utils.py
def hf_fs() -> "huggingface_hub.HfFileSystem":
    """Return a fresh HfFileSystem tagged with vLLM's library info."""
    return huggingface_hub.HfFileSystem(
        library_name="vllm",
        library_version=VLLM_VERSION,
    )

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.

Source code in vllm/transformers_utils/repo_utils.py
def try_get_local_file(
    model: str | Path, file_name: str, revision: str | None = "main"
) -> Path | Any | None:
    """
    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.
    """
    file_path = Path(model) / file_name
    if file_path.is_file():
        return file_path
    else:
        try:
            cached_filepath = try_to_load_from_cache(
                repo_id=model, filename=file_name, revision=revision
            )
            if isinstance(cached_filepath, str):
                return Path(cached_filepath)
            return cached_filepath
        except ValueError:
            ...
    return None