-
Notifications
You must be signed in to change notification settings - Fork 10
Improve download and cache performance; add resumable, parallel downloads and offline mode #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DePasqualeOrg
wants to merge
17
commits into
huggingface:main
Choose a base branch
from
DePasqualeOrg:optimizations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
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
8cb1eb3 to
49ff955
Compare
This was referenced Dec 27, 2025
49ff955 to
007d780
Compare
d4a7c1a to
ce5642b
Compare
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.
This PR includes significant improvements to download and cache performance as well as related functionality from swift-transformers#302, bringing swift-huggingface closer to feature parity with the Python huggingface_hub library. I've based these changes on the implementations in huggingface_hub (and swift-transformers where appropriate).
Changes
1. Skip API calls for cached files
When the revision is a commit hash (immutable), we skip all API calls if files are already cached. The
downloadSnapshotfunction checks the local cache first and returns immediately if all requested files are present.Python equivalent:
file_download.py:1082-10952. Parallel file downloads
Files are now downloaded concurrently using a task group with configurable concurrency (default: 8).
Python equivalent:
_snapshot_download.py:449-4553. Size-weighted download progress
Progress is weighted by file size instead of file count, providing accurate progress bars for downloads containing a mix of small config files and large model weights.
4. Automatic resume for interrupted downloads
Downloads automatically resume from where they left off using HTTP Range headers with a cache-first approach matching huggingface_hub:
cache/blobs/{etag}.incompletecache/blobs/{etag}on successsnapshots/{commit}/{filename}This enables cross-client resume—if a download starts in Python and gets interrupted, Swift can resume it (and vice versa), since incomplete files are stored in the same cache location with the same naming convention.
Python equivalent:
file_download.py:1850-1855(incomplete file handling),file_download.py:403-404(Range header)5. File locking
The original
FileLockmodule doesn't properly serialize concurrent downloads. I ported the filelock library used in huggingface_hub to Swift, which correctly serializes concurrent downloads, as I've demonstrated in a comparison test with the original FileLock module. The test andFileLockOriginalshould be removed before merging if we decide to use swift-filelock or a similar solution.Python equivalent:
file_download.py:1239-12516. Offline mode support
Added
useOfflineModeparameter and automatic network detection viaNetworkMonitor. In offline mode, returns all cached files without making network requests (matching huggingface_hub behavior).7. xet storage compatibility
Added
fetchFileMetadatato captureX-Linked-EtagandX-Repo-Commitheaders before CDN redirect. Uses same-host redirect handling matching huggingface_hub's_httpx_follow_relative_redirects.8. Linux support
Linux has full feature parity for caching (blob checks, file locking, cache structure) but lacks resume support due to API limitations. Fallback paths are included throughout.
Benchmark Results
I've added a separate benchmarks test target that will not run in CI and can be run with
RUN_BENCHMARKS=1 swift test --filter Benchmarks.Tested with
mlx-community/Qwen3-0.6B-Base-DQ5(~11 MB tokenizer.json).Tests
Added comprehensive test suite in
SnapshotDownloadTests.swift.