Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- fix: load cached pretrained models in offline mode by @emme1t in #2396
- fix(ci): install matching NVML headers for CUDA wheel builds

## [0.3.38]
Expand Down
40 changes: 33 additions & 7 deletions llama_cpp/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -2342,6 +2342,7 @@ def from_pretrained(
"""Create a Llama model from a pretrained model name or path.
This method requires the huggingface-hub package.
You can install it with `pip install huggingface-hub`.
With HF_HUB_OFFLINE=1, filenames are matched against cached files or a populated local_dir.

Args:
repo_id: The model repo id.
Expand All @@ -2354,7 +2355,12 @@ def from_pretrained(
Returns:
A Llama model."""
try:
from huggingface_hub import hf_hub_download, HfFileSystem
from huggingface_hub import (
hf_hub_download,
HfFileSystem,
snapshot_download,
constants,
)
from huggingface_hub.utils import validate_repo_id
except ImportError:
raise ImportError(
Expand All @@ -2364,12 +2370,32 @@ def from_pretrained(

validate_repo_id(repo_id)

hffs = HfFileSystem()

files = [
file["name"] if isinstance(file, dict) else file
for file in hffs.ls(repo_id, recursive=True)
]
if constants.HF_HUB_OFFLINE:
if (
local_dir is not None
and Path(local_dir).is_dir()
and any(Path(local_dir).iterdir())
):
snapshot_path = Path(local_dir)
else:
snapshot_path = Path(
snapshot_download(
repo_id=repo_id,
cache_dir=cache_dir,
local_files_only=True,
)
)
files = [
f"{repo_id}/{file.relative_to(snapshot_path).as_posix()}"
for file in snapshot_path.rglob("*")
if file.is_file()
]
else:
hffs = HfFileSystem()
files = [
file["name"] if isinstance(file, dict) else file
for file in hffs.ls(repo_id, recursive=True)
]

# split each file into repo_id, subfolder, filename
file_list: List[str] = []
Expand Down