huggingface / huggingface/diffusers

fix underlying issue with `test_from_save_pretrained_dtype_inference` is that the `model.to(dtype)` cast at

Open
#13,869 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
34.5k
Forks
7.3k
Avg merge
3d 3h
Merged PRs (30d)
91

Description

I think an underlying issue with `test_from_save_pretrained_dtype_inference` is that the `model.to(dtype)` cast at

https://github.com/huggingface/diffusers/blob/5d10b4de3b65b1debb8a94de3a0e3bfa51fc628a/tests/models/testing_utils/common.py#L484

is not dtype-aware (it will cast everything to `dtype`), but `from_pretrained(..., torch_dtype=dtype)` is dtype-aware (it respects `_keep_in_fp32_modules`, etc.). This causes the behavior of the two to diverge in several scenarios:

1. There are `_keep_in_fp32_modules` specified on the `model` (the more common case)
2. A non-persistent buffer like `inv_freq` is created with an explicit dtype (which is the case here)

which leads to a divergence between the outputs of `model` and `model_loaded`.

_Originally posted by @dg845 in https://github.com/huggingface/diffusers/pull/13862#discussion_r3359815645_

Cc: @dn6

Contributor guide

Open the contributing guide

Research direction

Start in tests/models/testing_utils/common.py around line 484 and read test_from_save_pretrained_dtype_inference. Compare the model.to(dtype) setup with from_pretrained(..., torch_dtype=dtype), focusing on _keep_in_fp32_modules and the explicitly typed inv_freq buffer. Done when the test setup and loading path agree in these scenarios without output divergence.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.