Skip to content

fix _get_model_file for loading right model in subfolder - #7140

Closed
MengqingCao wants to merge 1 commit into
huggingface:mainfrom
MengqingCao:fix_pretrain
Closed

MengqingCao wants to merge 1 commit into
huggingface:mainfrom
MengqingCao:fix_pretrain

Conversation

@MengqingCao

@MengqingCao MengqingCao commented Feb 29, 2024 •

Copy link
Copy Markdown
Contributor

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

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.

@MengqingCao MengqingCao changed the title fix _get_model_file for fix _get_model_file for loading right model in subfolder Feb 29, 2024
@sayakpaul
sayakpaul requested a review from yiyixuxu February 29, 2024 03:28
@sayakpaul

Copy link
Copy Markdown
Member

Isn't this a very extreme edge case?

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@MengqingCao

MengqingCao commented Feb 29, 2024 •

Copy link
Copy Markdown
Contributor Author

Isn't this a very extreme edge case?

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 examples. And both SDXL and SD models are popular.

@MengqingCao

Copy link
Copy Markdown
Contributor Author

Checking wheather subfolder is None before checking the presence of checkpoints in current path doesn't break branch with no subpaths either.

@MengqingCao MengqingCao closed this Mar 2, 2024
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]>
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.

Structure of unet in SDXL mismatch the structure of the official model

3 participants