Repository navigation
fix(diffusion): reuse warm HF cache instead of re-downloading models #2747
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """HF cache resolution helpers for the diffusers bridge. | ||
|
|
||
| Diffusers' ``from_pretrained`` resolves a bare repo id in *online* mode by | ||
| default: it issues per-file network requests to the Hub to revalidate ETags | ||
| before deciding whether to reuse the cache. Even a warm ``HF_HOME`` is then | ||
| re-validated over the network, and any ETag drift or partial cache turns into a | ||
| fresh download. The transformers bridge avoids this by pre-resolving the repo | ||
| to a local snapshot directory (see ``_resolve_model_dir`` in | ||
| ``nemo_automodel/_transformers/model_init.py``) and handing that directory to | ||
| HF, which then does zero network I/O. This module ports the same discipline to | ||
| the diffusion path. | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from nemo_automodel.shared.import_utils import safe_import | ||
|
|
||
| HF_HUB_AVAILABLE, _ = safe_import("huggingface_hub") | ||
|
|
||
| if HF_HUB_AVAILABLE: | ||
| from huggingface_hub import snapshot_download | ||
| else: | ||
| snapshot_download = None | ||
|
|
||
|
|
||
| def resolve_diffusion_model_dir(model_id: str) -> str: | ||
| """Resolve a HF repo id to a local snapshot directory. | ||
|
|
||
| Mirrors the transformers bridge so a warm ``HF_HOME`` is never re-validated | ||
| over the network. Local paths are returned unchanged. For repo ids, the | ||
| snapshot is downloaded once only when the cache is cold and the process is | ||
| online (``HF_HUB_OFFLINE`` unset); the returned directory is then resolved | ||
| with ``local_files_only=True`` so the subsequent ``from_pretrained`` call | ||
| performs no network I/O. | ||
|
|
||
| Args: | ||
| model_id: A HuggingFace repo id or a local filesystem path. | ||
|
|
||
| Returns: | ||
| A local directory path containing the model snapshot. When | ||
| ``huggingface_hub`` is unavailable, ``model_id`` is returned unchanged | ||
| so resolution falls back to HF's own handling. | ||
| """ | ||
| if os.path.isdir(model_id) or not HF_HUB_AVAILABLE: | ||
| return model_id | ||
|
|
||
| if os.environ.get("HF_HUB_OFFLINE", "0") != "1": | ||
| # Cold cache + online: fetch the snapshot once. | ||
| snapshot_download(model_id) | ||
| # Resolve (and require) the local snapshot without revalidating over the network. | ||
| return snapshot_download(model_id, local_files_only=True) |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| from nemo_automodel._diffusers import _hf_cache | ||
| from nemo_automodel._diffusers._hf_cache import resolve_diffusion_model_dir | ||
|
|
||
| MODULE = "nemo_automodel._diffusers._hf_cache" | ||
|
|
||
|
|
||
| def test_resolve_returns_local_path_unchanged(tmp_path): | ||
| # A directory that already exists should be returned verbatim without any | ||
| # Hub interaction. | ||
| with patch(f"{MODULE}.snapshot_download") as mock_sd: | ||
| assert resolve_diffusion_model_dir(str(tmp_path)) == str(tmp_path) | ||
| mock_sd.assert_not_called() | ||
|
|
||
|
|
||
| def test_resolve_offline_uses_cache_only(monkeypatch): | ||
| monkeypatch.setenv("HF_HUB_OFFLINE", "1") | ||
| monkeypatch.setattr(_hf_cache, "HF_HUB_AVAILABLE", True) | ||
|
|
||
| with patch(f"{MODULE}.snapshot_download", return_value="/cache/snapshot") as mock_sd: | ||
| resolved = resolve_diffusion_model_dir("some/repo-id") | ||
|
|
||
| # Offline: no cold-cache download, single cache-only resolution. | ||
| assert resolved == "/cache/snapshot" | ||
| mock_sd.assert_called_once_with("some/repo-id", local_files_only=True) | ||
|
|
||
|
|
||
| def test_resolve_online_downloads_then_resolves_locally(monkeypatch): | ||
| monkeypatch.delenv("HF_HUB_OFFLINE", raising=False) | ||
| monkeypatch.setattr(_hf_cache, "HF_HUB_AVAILABLE", True) | ||
|
|
||
| with patch(f"{MODULE}.snapshot_download", return_value="/cache/snapshot") as mock_sd: | ||
| resolved = resolve_diffusion_model_dir("some/repo-id") | ||
|
|
||
| assert resolved == "/cache/snapshot" | ||
| # Online: fetch once (cold cache), then resolve the local dir without revalidation. | ||
| assert mock_sd.call_count == 2 | ||
| assert mock_sd.call_args_list[0].args == ("some/repo-id",) | ||
| assert mock_sd.call_args_list[1].kwargs == {"local_files_only": True} | ||
|
|
||
|
|
||
| def test_resolve_passthrough_when_hub_unavailable(monkeypatch): | ||
| monkeypatch.setattr(_hf_cache, "HF_HUB_AVAILABLE", False) | ||
| assert resolve_diffusion_model_dir("some/repo-id") == "some/repo-id" | ||
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This assertion verifies the kwargs of the second
snapshot_downloadcall but doesn't check that"some/repo-id"was also passed as the positional arg. If someone accidentally dropped themodel_idargument from the second call, this test would still pass.