Skip to content
Closed
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
20 changes: 20 additions & 0 deletions mlx_lm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
from modelscope import snapshot_download
except ImportError:
raise ImportError("Run `pip install modelscope` to use ModelScope.")
_try_local_files_only = False
else:
from huggingface_hub import snapshot_download
from huggingface_hub.errors import LocalEntryNotFoundError

_try_local_files_only = True

# For large models with lots of files
resource.setrlimit(resource.RLIMIT_NOFILE, (2048, 4096))
Expand Down Expand Up @@ -249,6 +253,22 @@ def _download(

if not model_path.exists():
allow_patterns = allow_patterns or DEFAULT_ALLOW_PATTERNS
if _try_local_files_only:
# The common case is a repo that's already fully cached locally.
# Skip the network round-trip snapshot_download otherwise makes
# (an API call to check for repo updates) and only fall back to
# the online path if the local cache doesn't have everything.
try:
return Path(
snapshot_download(
path_or_hf_repo,
revision=revision,
allow_patterns=allow_patterns,
local_files_only=True,
)
)
except LocalEntryNotFoundError:
pass
model_path = Path(
snapshot_download(
path_or_hf_repo,
Expand Down