Skip to content

no_init_weights in from_pretrained should be optional #12153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

IrisRainbowNeko
Copy link
Contributor

@IrisRainbowNeko IrisRainbowNeko commented Aug 15, 2025

What does this PR do?

After diffusers>=0.33, no_init_weights is force applied in from_pretrained to speed up loading large models. But if user add new parameters to pretrained models (like Adapters), new parameters will not be initialized and may have NaNs to crash the model.

This PR:

  • Make no_init_weights in from_pretrained optional.
  • Add no_init_weights and init_weights option to from_single_file to align with from_pretrained.

Crash example:

down_blocks.0.attentions.0.transformer_blocks.0.conv1.conv_out.weight torch.Size([320, 160]) tensor(nan, grad_fn=<MeanBackward0>) tensor(nan, grad_fn=<StdBackward0>)
down_blocks.0.attentions.0.transformer_blocks.0.conv1.conv_out.bias torch.Size([320]) tensor(-3.5686e+27, grad_fn=<MeanBackward0>) tensor(4.5068e+28, grad_fn=<StdBackward0>)
down_blocks.0.attentions.0.transformer_blocks.0.conv1.conv_pool.weight torch.Size([320, 320]) tensor(-5.5766e+24, grad_fn=<MeanBackward0>) tensor(1.7845e+27, grad_fn=<StdBackward0>)
down_blocks.0.attentions.0.transformer_blocks.0.conv1.conv_pool.bias torch.Size([320]) tensor(-1.7843e+27, grad_fn=<MeanBackward0>) tensor(3.1918e+28, grad_fn=<StdBackward0>)

Use example:

from torch import nn
from diffusers import UNet2DConditionModel

class MyUNet2DConditionModel(UNet2DConditionModel):
    def __init__(...):
        self.adapter = nn.Linear(...)
        ...

model = MyUNet2DConditionModel.from_pretrained(..., init_weights=True)

Before submitting

Who can review?

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.

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.

1 participant