Skip to content

Conversation

@DePasqualeOrg
Copy link

@DePasqualeOrg DePasqualeOrg commented Dec 26, 2025

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 downloadSnapshot function checks the local cache first and returns immediately if all requested files are present.

Python equivalent: file_download.py:1082-1095

2. Parallel file downloads

Files are now downloaded concurrently using a task group with configurable concurrency (default: 8).

Python equivalent: _snapshot_download.py:449-455

3. 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:

  1. Download to cache/blobs/{etag}.incomplete
  2. Rename to cache/blobs/{etag} on success
  3. Create symlink in snapshots/{commit}/{filename}
  4. Copy from cache to destination

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 FileLock module 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 and FileLockOriginal should be removed before merging if we decide to use swift-filelock or a similar solution.

Python equivalent: file_download.py:1239-1251

6. Offline mode support

Added useOfflineMode parameter and automatic network detection via NetworkMonitor. In offline mode, returns all cached files without making network requests (matching huggingface_hub behavior).

7. xet storage compatibility

Added fetchFileMetadata to capture X-Linked-Etag and X-Repo-Commit headers 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).

Benchmark Before After Improvement
Cached file retrieval 521 ms 1.14 ms 457x faster
Fresh download 1956 ms 1360 ms 1.44x faster

Tests

Added comprehensive test suite in SnapshotDownloadTests.swift.

@DePasqualeOrg DePasqualeOrg changed the title Add download optimizations and offline mode support Download/cache optimizations and offline mode support Dec 26, 2025
@DePasqualeOrg DePasqualeOrg changed the title Download/cache optimizations and offline mode support Optimize download and cache performance, add offline mode support Dec 27, 2025
@DePasqualeOrg DePasqualeOrg changed the title Optimize download and cache performance, add offline mode support Optimize download and cache; add resumable, parallel downloads and offline mode Dec 27, 2025
@DePasqualeOrg DePasqualeOrg changed the title Optimize download and cache; add resumable, parallel downloads and offline mode Improve download and cache performance; add resumable, parallel downloads and offline mode Dec 27, 2025
@DePasqualeOrg DePasqualeOrg marked this pull request as draft January 5, 2026 12:50
@DePasqualeOrg DePasqualeOrg marked this pull request as ready for review January 5, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant