huggingface / huggingface/diffusers

StableDiffusionPipeline.from_pretrained() fails to load specified models, resulting in identical weights

Abierto
#9,332 3 comentarios 0 reacciones 0 asignados Ver en GitHub
bug stale
Lenguaje dominante
Python
Estrellas
34.5k
Forks
7.3k
Merge medio
3 d 3 h
PR fusionados (30 d)
91

Descripción

### 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_

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Comienza con StableDiffusionPipeline.from_pretrained y reproduce el comportamiento de carga usando las estructuras de directorios de modelA y modelB, model_index.json y las rutas de los componentes registradas en los logs. Compara los pesos del UNet cargado mientras compruebas la discrepancia entre los nombres de archivo safetensors y diffusion_pytorch_model.bin; se considera terminado cuando distintos modelos locales se cargan con pesos distintos.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python, pytorch
Área
machine-learning
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.