feat(modelartifacts): support bounded parallel Hugging Face file downloads - #11162
feat(modelartifacts): support bounded parallel Hugging Face file downloads#11162Dennisadira wants to merge 4 commits into
Conversation
…loads Closes mudler#11114. Snapshot materialization fetched every file through the sequential executor in DownloadFilesWithContext, so a repository split into many shards spent most of its wall clock in per-file request latency rather than moving bytes. Add DownloadFilesWithConcurrency, an errgroup with SetLimit, and keep DownloadFilesWithContext as a wrapper that passes a limit of 1. That leaves the two non-artifact callers (core/gallery and the model config loader) on exactly the path they had: tasks still run in slice order, and the first failure still returns before any later task starts. Only whole files run in parallel. A single file is never split, so the .partial resume machinery and the per-file SHA check in downloadTaskWithRetry are untouched. Two details the parallel path forced: - completedBytes becomes an atomic.Int64. Several AfterDownload hooks add to it while other files' progress callbacks read it; without this the race detector reports three races on the new specs. - The caller's status callback is serialized. The sequential path gave it an implicit guarantee of never being entered twice at once, and it belongs to the caller, so the executor keeps that promise rather than pushing locking onto every caller. AfterDownload is deliberately not serialized -- it does the verify-and-promote work that parallelism exists to overlap. Manifest order needed no work: each hook already writes its own manifest.Files slot by snapshot index, so entries stay in snapshot order whatever the completion order. A spec now pins that. The default is 1, unchanged behaviour. A shared models volume is often the bottleneck rather than the link, so raising it is a deployment decision; --artifact-download-concurrency and LOCALAI_ARTIFACT_DOWNLOAD_CONCURRENCY expose it on both `run` and `models install`. Not done here, per the issue: no chunk-level parallelism within a single file, and no throughput measurements across concurrency 1/2/4/8 -- that needs a representative sharded repo and a real link. Assisted-by: Claude:claude-opus-5 go-test gofmt Signed-off-by: Adira Denis Muhando <dennisadira@gmail.com>
localai-org-maint-bot
left a comment
There was a problem hiding this comment.
@mudler Good to merge from my review. The bounded executor preserves the sequential compatibility path, cancels sibling work on failure, serializes the legacy status callback, and the materializer uses atomic aggregate accounting while writing each manifest slot by stable snapshot index. The CLI flag is default-safe and documented. Fresh verification on exact head 3bb6c9ef: git diff --check passes, the full race-enabled pkg/modelartifacts suite passes, and all six new DownloadFilesWithConcurrency specs pass under -race. DCO passes. Repository Actions have not run on this fork head yet and still need maintainer authorization before merge.
| NoColor string `env:"NO_COLOR" hidden:""` | ||
| HFToken string `env:"HF_TOKEN" hidden:""` | ||
|
|
||
| ArtifactDownloadConcurrency int `env:"LOCALAI_ARTIFACT_DOWNLOAD_CONCURRENCY" help:"How many files of a Hugging Face model artifact to download at once. 1 (the default) downloads sequentially. Raising it helps repositories split into many shards on a fast link, at the cost of more concurrent load on the models volume" group:"storage" default:"1"` |
There was a problem hiding this comment.
no need to document it only for huggingface, this has effect for any url, no?
| BackendsPath string `env:"LOCALAI_BACKENDS_PATH,BACKENDS_PATH" type:"path" default:"${basepath}/backends" help:"Path containing backends used for inferencing" group:"backends"` | ||
| BackendsSystemPath string `env:"LOCALAI_BACKENDS_SYSTEM_PATH,BACKEND_SYSTEM_PATH" type:"path" default:"/var/lib/local-ai/backends" help:"Path containing system backends used for inferencing" group:"backends"` | ||
| ModelsPath string `env:"LOCALAI_MODELS_PATH,MODELS_PATH" type:"path" default:"${basepath}/models" help:"Path containing models used for inferencing" group:"storage"` | ||
| ArtifactDownloadConcurrency int `env:"LOCALAI_ARTIFACT_DOWNLOAD_CONCURRENCY" help:"How many files of a Hugging Face model artifact to download at once. 1 (the default) downloads sequentially. Raising it helps repositories split into many shards on a fast link, at the cost of more concurrent load on the models volume" group:"storage" default:"1"` |
There was a problem hiding this comment.
would actually make sense to wire it in the runtime settings as well, so users can also configure it via WebUI
|
@mudler I pushed the two requested minor changes to the contributor branch in |
Follow-up to review feedback on mudler#11162: - The CLI flag and docs no longer describe the limit as Hugging Face specific. It applies to any artifact source, as @mudler pointed out. - artifact_download_concurrency is now a persisted runtime setting and is editable from the WebUI, so it can be changed without a restart. The manager's limit becomes an atomic.Int64 behind SetDownloadConcurrency, because a live runtime setting can be updated while a materialization is already in flight. Injected materializers stay compatible through an optional setter interface, so a manager that does not implement it is simply left alone. Verified before taking this on: go build, go vet and go test -race all pass for pkg/modelartifacts, pkg/downloader and core/config. The React UI builds with vite, artifact_download_concurrency is present in the built Settings chunk, and eslint reports the same 8 pre-existing warnings on Settings.jsx as it does without the change. Implementation contributed by localai-org-maint-bot on the review thread; reviewed, verified and signed off by me. Assisted-by: Codex:gpt-5 Assisted-by: Claude:claude-opus-5 go-test vite eslint Signed-off-by: Adira Denis Muhando <dennisadira@gmail.com>
4951ca1 to
c8132be
Compare
Closes #11114.
Problem
Snapshot materialization fetched every file through the sequential executor in
DownloadFilesWithContext(pkg/downloader/download_plan.go), so a repository split into many shards spent most of its wall clock in per-file request latency rather than moving bytes.What changed
DownloadFilesWithConcurrencyruns up to N whole-file transfers at once via anerrgroupwithSetLimit.DownloadFilesWithContextstays as a wrapper passing a limit of 1, so the two non-artifact callers —core/gallery/models.goandcore/config/model_config_loader.go— keep exactly the behaviour they had: tasks still run in slice order, and the first failure still returns before any later task starts.Only whole files run in parallel. A single file is never split, so the
.partialresume machinery and the per-file SHA check indownloadTaskWithRetryare untouched.Two details the parallel path forced, both worth a look during review:
completedBytesis now anatomic.Int64(pkg/modelartifacts/materializer.go). SeveralAfterDownloadhooks add to it while other files' progress callbacks read it. This is not a precaution — with a plainint64the race detector reports three races on the new specs.AfterDownloadis deliberately not serialized: it does the verify-and-promote work that parallelism exists to overlap, so hooks must be safe to run concurrently.Manifest ordering needed no code change — each hook already writes its own
manifest.Filesslot by snapshot index, so entries stay in snapshot order regardless of completion order — but nothing pinned that, so there is now a spec for it.Configuration
Default is
1, i.e. current behaviour. A shared models volume is often the bottleneck rather than the link, so raising this is a deployment decision rather than something to assume.--artifact-download-concurrencyLOCALAI_ARTIFACT_DOWNLOAD_CONCURRENCY1Available on both
local-ai runandlocal-ai models install; documented indocs/content/reference/cli-reference.md.Verification
go build,go vet, andgo test -raceclean on current master forpkg/downloader,pkg/modelartifacts, andcore/cli. Ten new specs (6 inpkg/downloader, 4 inpkg/modelartifacts).I checked the specs actually catch the bugs they claim to, by reverting each part and confirming the expected failure:
records the manifest in snapshot orderfails: "files never overlapped, so this proves nothing about ordering"group.SetLimit(concurrency)removedatomic.Int64back to a plainint64DATA RACEwarnings under-raceThe concurrency specs assert on the peak number of simultaneous in-flight requests observed by the test server, so they distinguish configured concurrency from actual concurrency, and a limit of 1 (or 0, or negative) is asserted to never overlap at all.
Throughput
Measured locally against an instrumented server rather than a real Hub repo, in the two regimes that bracket real behaviour. 32 files at 64 KiB with a fixed per-request delay, and 16 files at 2 MiB against a single server-wide byte budget shared across all in-flight responses:
Near-linear while per-file round-trip latency dominates, and exactly flat once the link or the volume is saturated — parallelism buys nothing there, it just multiplies concurrent load. That asymmetry is the reason the default is 1: the win depends entirely on which side of it a given deployment sits, which is not something LocalAI can infer.
Against the real Hub
End-to-end through the materializer, fetching 11 small files (0.7 MiB total) from
sentence-transformers/all-MiniLM-L6-v2, four rounds with the concurrency levels interleaved so link drift hits both arms equally:~3.0× median, consistent in both orderings. This is a small-file, latency-dominated repo, which is exactly the case the change targets.
Three caveats I would rather state than bury:
download stalled: no data received for 1m0sandhttp2: timeout awaiting response headers. That failed at concurrency 1 as well, i.e. on the untouched sequential path, so it is a Hub/CDN condition rather than anything this PR introduces — but it does mean I have no real-link data for the bandwidth-bound regime, only the synthetic figures above.This is not the "representative sharded HF model" the issue asks for — a real 70 GB multi-shard fetch is not something I can run reproducibly here. Happy to measure a specific repo if you have one in mind.
Not done here