Implementation:Huggingface Optimum Download Model From HF
Appearance
Overview
Downloads model artifacts from the Hugging Face Hub to the local filesystem, returning the path to the cached model snapshot. Supports selective downloading to skip weight files when only the model configuration is needed.
Source
| Property | Value |
|---|---|
| File | optimum/fx/parallelization/utils.py
|
| Lines | L359-416 |
| Module | optimum.fx.parallelization.utils
|
Signature
def download_model_from_hf(
model_name_or_path: str,
cache_dir: Optional[str],
revision: Optional[str] = None,
local_files_only: bool = False,
skip_download_weights: bool = False,
) -> str:
Import
from optimum.fx.parallelization.utils import download_model_from_hf
Input / Output
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | model_name_or_path | str |
Hugging Face Hub model identifier (e.g., meta-llama/Llama-2-7b-hf) or a local filesystem path. |
| Input | cache_dir | Optional[str] |
Local directory for caching downloaded files. |
| Input | revision | Optional[str] |
Git revision (branch, tag, or commit hash) to download. Defaults to None (latest). |
| Input | local_files_only | bool |
If True, only look for files in the local cache. Defaults to False. |
| Input | skip_download_weights | bool |
If True, skip downloading safetensors weight files. Defaults to False. |
| Output | (return) | str |
Local filesystem path to the downloaded model snapshot directory. |
Behavior
The function performs the following steps:
- Check for local path: If model_name_or_path is a local directory, return it directly without downloading.
- Determine files to ignore: If skip_download_weights is True, build a list of patterns to ignore safetensors weight files during download.
- Call huggingface_hub.snapshot_download: Download the model snapshot from the Hub with the specified parameters (cache directory, revision, ignore patterns).
- Return snapshot path: Return the local path where the snapshot was cached.
Example Usage
from optimum.fx.parallelization.utils import download_model_from_hf
# Download full model (weights + config)
model_path = download_model_from_hf(
model_name_or_path="meta-llama/Llama-2-7b-hf",
cache_dir="/tmp/model_cache",
)
# Download config only (skip weights for meta-device init)
config_path = download_model_from_hf(
model_name_or_path="meta-llama/Llama-2-7b-hf",
cache_dir="/tmp/model_cache",
skip_download_weights=True,
)
Dependencies
- huggingface_hub.snapshot_download - Core download functionality from the Hugging Face Hub library.
- os.path.isdir - Used to detect local model paths.
Related
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment