From cae94e8348bfb3c9d7e02f5e81991a8bd0b924cd Mon Sep 17 00:00:00 2001 From: sohailshk <122166523+sohailshk@users.noreply.github.com> Date: Tue, 1 Dec 2026 21:46:05 +0530 Subject: [PATCH] fix: load cached projector files offline in the multimodal chat handlers' from_pretrained Llava15ChatHandler.from_pretrained and MTMDChatHandler.from_pretrained still list the repo through HfFileSystem, so with HF_HUB_OFFLINE=1 they raise OfflineModeIsEnabled even when the projector file is cached or already in local_dir. Use the cached snapshot or a populated local_dir, as Llama.from_pretrained does since #2396. --- CHANGELOG.md | 1 + llama_cpp/llama_chat_format.py | 78 ++++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa7e917bd..ebc2f1f975 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- fix: load cached projector files offline in the multimodal chat handlers' from_pretrained by @sohailshk in #XXXXX - fix: load cached pretrained models in offline mode by @emme1t in #2396 - fix(ci): install matching NVML headers for CUDA wheel builds diff --git a/llama_cpp/llama_chat_format.py b/llama_cpp/llama_chat_format.py index 62e2364157..5fcb716104 100644 --- a/llama_cpp/llama_chat_format.py +++ b/llama_cpp/llama_chat_format.py @@ -3195,7 +3195,12 @@ def from_pretrained( from pathlib import Path try: - from huggingface_hub import hf_hub_download, HfFileSystem # type: ignore + from huggingface_hub import ( # type: ignore + hf_hub_download, + HfFileSystem, + snapshot_download, + constants, + ) from huggingface_hub.utils import validate_repo_id # type: ignore except ImportError: raise ImportError( @@ -3205,12 +3210,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) # type: ignore - ] + 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.name}" + for file in snapshot_path.iterdir() + if file.is_file() + ] + else: + hffs = HfFileSystem() + files = [ + file["name"] if isinstance(file, dict) else file + for file in hffs.ls(repo_id) # type: ignore + ] # split each file into repo_id, subfolder, filename file_list: List[str] = [] @@ -3703,7 +3728,12 @@ def from_pretrained( from pathlib import Path try: - from huggingface_hub import hf_hub_download, HfFileSystem # type: ignore + from huggingface_hub import ( # type: ignore + hf_hub_download, + HfFileSystem, + snapshot_download, + constants, + ) from huggingface_hub.utils import validate_repo_id # type: ignore except ImportError: raise ImportError( @@ -3713,12 +3743,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) # type: ignore - ] + 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.name}" + for file in snapshot_path.iterdir() + if file.is_file() + ] + else: + hffs = HfFileSystem() + files = [ + file["name"] if isinstance(file, dict) else file + for file in hffs.ls(repo_id) # type: ignore + ] file_list: List[str] = [] for file in files: