huggingface / huggingface/diffusers
StableDiffusionPipeline.from_pretrained() fails to load specified models, resulting in identical weights
- Vorherrschende Sprache
- Python
- Sterne
- 34.5k
- Forks
- 7.3k
- Ø Merge
- 3 T. 3 Std.
- Gemergte PRs (30 T.)
- 91
Beschreibung
### Describe the bug
## Issue Summary
The `from_pretrained()` method of StableDiffusionPipeline fails to correctly load the specified models on local directory. Instead, it appears to use the same (likely default) weights for different model paths. This results in identical models being used even when attempting to load different models.
## Steps to Reproduce
1. Prepare two different Stable Diffusion models (modelA and modelB), ensuring each model directory contains all necessary components:
- unet (directory containing model files)(not safetensors. diffusion_pytorch_model.bin)
- vae (directory containing model files)
- text_encoder (directory containing model files)
- tokenizer (directory containing model files)
- scheduler (directory containing configuration)
- model_index.json
2. Execute the following code:
## Expected Behavior
The `from_pretrained()` method should correctly load two different specified models, each with their unique weights.
## Actual Behavior
The `from_pretrained()` method does not load the specified models as expected. Instead, both instances have identical weights, suggesting that the correct models are not being loaded.
### Reproduction
```python
import torch
from diffusers import StableDiffusionPipeline,StableDiffusionXLPipeline,StableDiffusion3Pipeline
import matplotlib.pyplot as plt
#directory of model in local
path_modelA = "./models/modelA"
path_modelB = "./models/modelB"
def load_model(model_path):
pipe = StableDiffusionPipeline.from_pretrained(
model_path,
torch_dtype=torch.float16,
use_safetensors=True,
local_files_only=True,
safety_checker=None
)
return pipe.to("cuda")
pipe1 = load_model(path_modelA)
pipe2 = load_model(path_modelB)
print("Models are the same:", torch.allclose(pipe1.unet.conv_in.weight, pipe2.unet.conv_in.weight))
print("First few weights of model A:", pipe1.unet.conv_in.weight.flatten()[:10])
print("First few weights of model B:", pipe2.unet.conv_in.weight.flatten()[:10])
#generate in pipeline
prompt = "a cat"
pipe = StableDiffusionPipeline.from_pretrained(path_modelA, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
pipe.safety_checker = None
generator = torch.Generator("cuda").manual_seed(42)
image = pipe(prompt, generator=generator).images[0]
image.save("generated_image1.png")
pipe = StableDiffusionPipeline.from_pretrained(path_modelB, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
pipe.safety_checker = None
generator = torch.Generator("cuda").manual_seed(42)
image = pipe(prompt, generator=generator).images[0]
image.save("generated_image2.png")
```
### Logs
```shell
sh-5.1# python test.py
/usr/local/lib/python3.10/dist-packages/xformers/ops/fmha/flash.py:211: FutureWarning: `torch.library.impl_abstract` was renamed to `torch.library.register_fake`. Please use that instead; we will remove `torch.library.impl_abstract` in a future version of PyTorch.
@torch.library.impl_abstract("xformers_flash::flash_fwd")
/usr/local/lib/python3.10/dist-packages/xformers/ops/fmha/flash.py:344: FutureWarning: `torch.library.impl_abstract` was renamed to `torch.library.register_fake`. Please use that instead; we will remove `torch.library.impl_abstract` in a future version of PyTorch.
@torch.library.impl_abstract("xformers_flash::flash_bwd")
Loading pipeline components...: 0%| | 0/6 [00:00 by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .
Loading pipeline components...: 0%| | 0/6 [00:00 by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .
Models are the same: True
First few weights of model A: tensor([-0.0307, 0.0853, 0.0953, -0.0312, -0.2228, 0.1432, -0.0234, -0.0179,
0.0331, 0.0573], device='cuda:0', dtype=torch.float16,
grad_fn=)
First few weights of model B: tensor([-0.0307, 0.0853, 0.0953, -0.0312, -0.2228, 0.1432, -0.0234, -0.0179,
0.0331, 0.0573], device='cuda:0', dtype=torch.float16,
grad_fn=)
Loading pipeline components...: 0%| | 0/7 [00:00
- Using distributed or parallel set-up in script?:
### Who can help?
_No response_
Beitragsleitfaden
Rechercherichtung
Start with StableDiffusionPipeline.from_pretrained and reproduce the loading behavior using the modelA and modelB directory layouts, model_index.json, and the logged component paths. Compare the loaded UNet weights while checking the safetensors and diffusion_pytorch_model.bin filename mismatch; done means distinct local models load with distinct weights.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python, pytorch
- Bereich
- machine-learning
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 30/100