Repository navigation
fix _get_model_file for loading right model in subfolder - #7140
Closed
MengqingCao wants to merge 1 commit into
Closed
MengqingCao wants to merge 1 commit into
MengqingCao wants to merge 1 commit into
Conversation
Member
|
Isn't this a very extreme edge case? |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Contributor
Author
This may indeed be a rare case, but as far as I know, it's the case for both SDXL and stable-diffusion-v1-5 at least in |
Contributor
Author
|
Checking wheather |
yingguo-trt
added a commit
to NVIDIA/Model-Optimizer
that referenced
this pull request
Oct 26, 2026
…2971) ### What does this PR do? Type of change: Bug fix Loading a Diffusers component with `from_pretrained(parent, subfolder="unet")` finds its weights in the component directory, but ModelOpt still looks for `modelopt_state.pth` in the parent directory. The load can succeed while dropping the saved quantizers. Pass the component subfolder through ModelOpt's state lookup for local directories and existing Hub cache entries. Calls without `subfolder` and checkpoint saving retain their existing behavior; no download behavior is added. ### Usage ```python import modelopt.torch.opt as mto from diffusers import UNet2DConditionModel mto.enable_huggingface_checkpointing() # A quantized UNet was saved with save_pretrained("pipeline/unet"). restored = UNet2DConditionModel.from_pretrained( "pipeline", subfolder="unet", local_files_only=True ) ``` ### Testing Local CPU environment: Python 3.12.10, PyTorch 2.12.0+cpu, Diffusers 0.40.0, Transformers 5.5.4. - Existing `tests/unit/torch/opt/plugins/test_diffusers_save_load.py`: **1 passed**. - Existing `tests/unit/torch/opt/plugins/test_transformers_save_load.py::test_causal_lm_save_restore`: **2 passed** (`LlamaForCausalLM` and `AutoModelForCausalLM`). - Standalone tiny UNet INT8 reproducer, before and after the fix: direct-directory loading restores 30 enabled quantizers in both versions. Subfolder loading restores 0 before the fix and 30 afterward; its maximum absolute output difference from the original quantized model changes from approximately 0.020612 to 0. - `pre-commit run --files modelopt/torch/opt/plugins/diffusers.py modelopt/torch/opt/plugins/huggingface.py`: **passed**. No test files were added or modified. Fix validation uses a parent directory without component weights. Hub loading, GPU inference, and other Diffusers architectures were not validated. ### Before your PR is "*Ready for review*" Contributor and security guidance reviewed; the commit is signed and signed off. - Is this change backward compatible?: ✅ Existing calls without `subfolder` retain their behavior. - If you copied code from any other sources or added a new PIP dependency, did you follow guidance in `CONTRIBUTING.md`: N/A — neither. - Did you write any new necessary tests?: ❌ No new checked-in tests; existing tests and the standalone reproducer were run as described above. - Did you update Changelog?: N/A — no new feature or public API change; affected prior released versions have not been established. - Did you get Claude approval on this PR?: ❌ Not yet reviewed by Claude. ### Additional Information This change does not alter Diffusers' weight selection when both the parent and component directory contain same-named weights; that separate directory-layout edge case is discussed in huggingface/diffusers#7140. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Model loading now correctly handles models stored in subfolders, including when retrieving associated optimization state from local directories or the Hugging Face Hub. Existing loading behavior remains unchanged when no subfolder is specified. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: yingguo-trt <[email protected]>
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.
What does this PR do?
Fixes #7138
In the _get_model_file function in
src/diffusers/utils/hub_utils.py, the if branch shadows the case where the pytorch checkpoint file exists in both a folder and a subfolder. And this causes the error in #7138 .Before submitting
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@sayakpaul @yiyixuxu @DN6
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.