Skip to content

Studio: fetch up to four Hub files at once over HTTP when partials resume - #15781

Merged
danielhanchen merged 4 commits into
mainfrom
perf/download-concurrency
Oct 29, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
perf/download-concurrency

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

What changes

The Studio Hub download worker (studio/backend/hub/workers/hf_download.py) passed max_workers=1 to every snapshot_download call, so a model, GGUF variant, scoped snapshot or dataset was fetched one file at a time. Over HTTP each file paid its own HEAD and request latency in series and was limited to what one connection delivers.

Over HTTP the worker now fetches up to four files at once, but only where an interrupted partial resumes: on huggingface_hub up to 1.17 (whose stock writer appends to a shared .incomplete), or on 1.18+ when the worker restored the resumable writer at import (restore_resumable_partials(), which needs a local filesystem whose lock provably excludes a second writer). Over Xet, on 1.18+ where the resumable writer was not restored (for example Windows or a network mount), or with an unreadable huggingface_hub version, it stays at one file at a time exactly as before. In practice: Linux and macOS on a local filesystem overlap files with any supported huggingface_hub; Windows overlaps only on the Python 3.9 pin (huggingface_hub 0.36.2) and stays serial on 1.18+.

Why this is safe for resume: the original reason for max_workers=1 was that resume needs a single sequential writer per partial. That still holds per file: huggingface_hub takes a per-blob lock and each file is one sequential writer, so every partial resumes from its own length. The gate means every partial on disk resumes after a kill. What a kill can lose is the chunk each stream is holding in memory before it is written (DOWNLOAD_CHUNK_SIZE, 10 MiB), so up to about 40 MiB with four streams instead of about 10 MiB with one. Xet is unchanged because hf_xet already fans out inside a file, its buffers scale per file (the memory caps from #7742), and a killed Xet file restarts from zero.

Nothing else changes: the transport environment, token handling, the purge and marker logic, verification, cancellation (SIGKILL of the worker process), the error raised (the first failing file in listing order) and the byte-based progress that the download manager reads from the cache.

Side effects of overlapping files, all bounded:

  • Peak memory of the worker rises by about 70 MB: each stream reads in 10 MiB chunks (DOWNLOAD_CHUNK_SIZE), so the extra memory scales with the number of streams, not with shard size.
  • The download manager's progress is the sum of finalized blob bytes plus the largest reading per partial, so up to four partials add up the same way one does; a finalized blob still supersedes its own partial, so nothing is counted twice. The stall watchdog sums the partials the worker holds open, so any stream making progress keeps it armed.
  • A cancel or a stall kill is still SIGKILL of the worker. It can leave up to four partials instead of one; every one of them resumes on the next attempt, which is what the gate guarantees.
  • When one file fails, the files already in flight finish before the worker exits with the error, so an error can surface later than it does today (those files are kept).
  • The number of Hub requests is unchanged. The Hub counts requests per 5-minute window, so a repo with very many small files spends the same budget over a shorter time.

Results

These were measured against a local fake Hub serving model_info, the tree listing and resolve HEAD/GET with Range. Each connection had a bandwidth cap and every request had added latency. The real worker script ran end to end as a subprocess. Base and head were interleaved, with 3 runs per arm and a second base arm as the A/A check.

Fixture Link base base (A/A) head
LLM: 4 x 48 MB shards, 8 MB tokenizer, 8 small files (hub 0.36.2) 20 MB/s per conn, 50 ms 12.90 s [12.80, 13.06] 12.91 s [12.82, 13.05] 3.97 s [3.95, 3.98]
Same (hub 1.33.0) 20 MB/s per conn, 50 ms 13.13 s [13.11, 13.22] 13.12 s [13.01, 13.27] 4.10 s [4.08, 4.20]
Diffusers layout: 19 files, 3 weights 20 MB/s per conn, 50 ms 8.71 s [8.60, 8.82] 8.69 s [8.60, 8.83] 4.57 s [4.55, 4.65]
30 small files 20 MB/s per conn, 80 ms 6.86 s [6.81, 6.94] 6.91 s [6.76, 7.07] 2.30 s [2.27, 2.36]
LLM, link saturated by one stream 40 MB/s per conn and 40 MB/s total, 50 ms 7.52 s [7.50, 7.93] 7.92 s [7.49, 8.08] 6.12 s [6.09, 6.23]
LLM, fast link 400 MB/s per conn, 5 ms 1.56 s [1.49, 1.74] 1.51 s [1.49, 2.86] 0.75 s [0.74, 1.20]

The values are medians, with the [min, max] range in brackets. In every run both arms made the same requests: 2 metadata GETs (plus 1 tree GET on hub 1.x) and exactly one HEAD and one GET per file. They also sent the same bytes. The snapshot directories had an identical sha256 digest across all arms, and no .incomplete file was left behind. The peak number of concurrent GETs was 1 on base and up to 4 on head.

A separate re-run on huggingface_hub 1.33.0 (LLM fixture, same link, 3 interleaved runs per arm) gave base 12.82 s [12.82, 13.05], base A/A 12.83 s [12.79, 12.86] and head 4.05 s [4.05, 4.09], with the same request counts, bytes and digest in every run. Peak worker RSS in those runs was 72 to 83 MB on base and 143 to 153 MB on head.

Resume after SIGKILL: the worker was killed partway through the LLM fixture, then run again. On both hub 0.36.2 and 1.33.0, and on both arms, the bytes left in partials plus the bytes the second run fetched add up exactly to the shard total (201,326,592). Nothing was fetched twice. The final snapshot digest was identical. For example, head with a kill at 3.5 s left 4 partials holding 157,286,400 bytes, and the rerun fetched the remaining 44,040,192.

Re-check on the current head (kill triggered once the fake Hub had sent 48 MiB, 4 x 32 MiB shards at about 8 MB/s per connection, real worker subprocess):

hub arm partials after kill bytes in partials finalized refetched on rerun partial + finalized + refetched == total rerun
0.36.2 base 1 10,485,760 33,554,432 90,177,736 yes 12.33 s
0.36.2 head 4 41,943,040 0 92,274,888 yes 3.77 s
1.33.0 base 1 10,485,760 33,554,432 90,177,736 yes 12.19 s
1.33.0 head 4 41,943,040 0 92,274,888 yes 3.92 s

Final files were byte-identical and no .incomplete was left in every run.

Offline (HF_HUB_OFFLINE=1 on a cached repo): base and head give the same output and exit 0.

Tests

  • New studio/backend/tests/test_hf_download_file_concurrency.py:
    • checks that Xet stays serial, and that HTTP overlaps files only when partials resume (hub up to 1.17, or 1.18+ with the restored writer installed; stock 1.18+ and an unreadable version stay serial);
    • checks through the AST that all four snapshot_download calls use the transport budget;
    • runs the real worker against an in-process fake Hub and checks byte-identical files, one GET per file, no leftover partials and at least 2 GETs in flight at once.
    • On base 8 of 8 tests fail; on head 8 of 8 pass, under huggingface_hub 0.36.2 and 1.33.0.
  • 18 neighbouring download test files (transport, scoped jobs, token handling, size ceiling, Xet fallback, training downloads and others): 505 passed on both base and head, with the same failing IDs (none).
  • A second sweep over 20 neighbouring files (resumable partials, heartbeat, stall watchdog, snapshot reuse, model services, scoped jobs, transport) on huggingface_hub 0.36.2 and 1.33.0: the same failing IDs on base and head apart from the new file. The failures present on both arms (test_delete_variant_* in hub/tests/test_model_services.py and test_optional_loader_retries_with_gpu_init_disabled) do not touch the worker.
  • Re-run on the current head over 63 neighbouring download / Xet / partial / heartbeat / stall / snapshot / transport test files: 2114 passed on huggingface_hub 0.36.2 and 2041 on 1.33.0, with identical failing IDs on base and head under each.
  • py_compile on Python 3.9 and 3.10 for every touched file.

Not covered

  • The real Hugging Face Hub and CDN were not used. Per-connection throughput there depends on the network, so the gain on a fast uncapped link is mostly from hiding request latency on the small files.
  • Windows and macOS were not run. On Windows with huggingface_hub 1.18+ the gate keeps the serial path, because partials there are not resumable.
  • Xet is deliberately unchanged and was not measured.
  • Peak memory was measured with 48 MB shards; that it stays flat with multi-GB shards follows from the fixed read chunk size and was not measured.
  • A real 429 from the Hub was not exercised.

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 28, 2026
@danielhanchen danielhanchen self-assigned this Oct 29, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T09:34:15.939443Z 3113094 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 3113094a82

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 98b37c2 into main Oct 29, 2026
1 of 38 checks passed
@danielhanchen
danielhanchen deleted the perf/download-concurrency branch October 29, 2026 09:57
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