Try local_files_only first in load() to skip avoidable HF Hub round-trips - #1571
Closed
Vlor999 wants to merge 1 commit into
Closed
Try local_files_only first in load() to skip avoidable HF Hub round-trips#1571Vlor999 wants to merge 1 commit into
Vlor999 wants to merge 1 commit into
Conversation
…-trips _download() (used by both load() for model weights and load_tokenizer() for tokenizer files) calls huggingface_hub.snapshot_download() without local_files_only=True. Even when a repo is fully cached locally, snapshot_download makes a network round-trip (a repo_info/model_info API call) to check for updates before touching the cache. Since load() calls _download() twice per invocation (once for weights, once for tokenizer files), this costs ~400-450ms on every single load() call for an already-cached model - paid by every CLI invocation (mlx_lm.generate, mlx_lm.chat) and every library user, not just first-time downloads. This tries snapshot_download(..., local_files_only=True) first (near-instant when the cache is complete) and only falls back to the network-enabled call on LocalEntryNotFoundError (the genuine first-download/incomplete-cache case). ModelScope users are unaffected (different backend, left on the existing always-network path since I can't validate local_files_only semantics there). Measured on an M4 Pro (5 runs/config, mean +/- stdev): - Qwen2.5-0.5B-Instruct-4bit: 0.355s -> 0.178s (-49.8%) - Llama-3.2-1B-Instruct-4bit: 0.592s -> 0.412s (-30.5%) - gpt-oss-20b-MXFP4-Q8: 3.765s -> 3.544s (-5.9%, weight loading dominates) Full benchmark methodology, raw data, and plots in the companion issue/PR.
Author
|
Superseded by #1650, which rebuilds this on current |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1570.
Summary
_download()(used by bothload()for model weights andload_tokenizer()for tokenizer files) calledhuggingface_hub.snapshot_download()withoutlocal_files_only=True. Even with a complete local cache,snapshot_downloadperforms an API round-trip (repo_info/model_info) to check for updates before touching the cache. Sinceload()calls_download()twice per invocation (weights + tokenizer files), this cost was paid twice on every single call — for the CLI (mlx_lm.generate,mlx_lm.chat,mlx_lm.server) and every library user, not just first-time downloads.This PR makes
_download()trysnapshot_download(..., local_files_only=True)first (near-instant when the cache is complete) and only fall back to the network-enabled call onLocalEntryNotFoundError(the genuine first-download / incomplete-cache case). ModelScope users are unaffected — left on the existing always-network path since I couldn't validatelocal_files_onlysemantics for that backend.Measurements (M4 Pro, 5 runs/config, mean ± stdev)
For an isolated cache hit,
snapshot_download(..., local_files_only=True)on these repos takes 0.4-1.6ms, confirming the ~200-230ms per call removed was pure avoidable network latency rather than any real cache-validation work.Note on
load_tokenizer: after this fix, the new dominant cost ofload()(profiled withcProfile) istransformers.AutoTokenizer.from_pretraineditself (Tokenizer.__setstate__+ acopy.deepcopyof the tokenizer object, ~280ms combined for Llama-3.2-1B). That's upstreamtransformers/tokenizerscode, out of scope for this PR, but worth flagging for anyone looking atload()latency next.Test plan
python -m pytest tests/test_utils.py tests/test_generate.py tests/test_tokenizers.py tests/test_prompt_cache.py— 62 passed, 6 subtests passed, no changes needed.black --check mlx_lm/utils.py— clean on the diff (an unrelated pre-existing block elsewhere in the file would reformat under a newer black, untouched here).LocalEntryNotFoundErroron cache miss, falls through to the network-enabled call).