FIX: Load HuggingFace models without blocking the event loop#2211
Merged
romanlutz merged 3 commits intoJul 17, 2026
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 52d2518d-ebe5-4888-8189-6146b6503448
behnam-o
approved these changes
Jul 16, 2026
behnam-o
left a comment
Contributor
There was a problem hiding this comment.
little question/nit but otherwise great. thanks!
behnam-o
approved these changes
Jul 16, 2026
The switch to huggingface_hub.snapshot_download left get_available_files and the download_files_async/download_file_async/download_chunk_async chain with no callers. Remove them and their tests. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 18c92f59-1265-4426-b826-7c407c6e8c7d
romanlutz
enabled auto-merge
July 17, 2026 03:49
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.
Description
HuggingFaceChatTargetpreviously downloaded and loaded models using blocking calls that ran directly on the asyncio event loop. For large or slow model pulls this stalled every other concurrent task until the download and weight loading finished. This change makes model loading non-blocking.Key changes:
download_specific_files_asyncnow delegates tohuggingface_hub.snapshot_downloadrun viaasyncio.to_thread(...), so the download happens off the event loop. This also replaces the bespoke URL/chunked-download logic with the canonical HF snapshot API, which handles allow-patterns, auth tokens, and resume for us.HuggingFaceChatTarget.load_model_and_tokenizer_asyncnow loads the downloaded snapshot through the existing_load_from_pathhelper instead of duplicatingfrom_pretrainedcalls, consolidating the two load paths (and removing a no-longer-neededcast).get_available_filesand thedownload_files_async/download_file_async/download_chunk_asyncchain) and their unused imports, sincesnapshot_downloadsupersedes them.download_specific_files_asyncis the only remaining helper.Tests and Documentation
Tests:
tests/unit/common/test_hf_model_downloads.pyto assertsnapshot_downloadis invoked with the expectedrepo_id,allow_patterns,token, andlocal_dir, and removed the tests covering the deleted helpers.tests/unit/prompt_target/target/test_huggingface_chat_target.pyso theasyncio.create_taskmock closes the coroutine (avoids "coroutine was never awaited" warnings).uv run pyteston both files passes.JupyText:
uv run jupytext --execute --set-kernel python3 --to notebook doc/code/targets/use_huggingface_chat_target.py-- this downloads a realQwen/Qwen2-0.5B-Instructmodel through the newsnapshot_download+_load_from_pathpath and runs inference end to end.doc/code/targets/11_message_normalizer.py..ipynboutputs are included in this PR.