Skip to content
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

Integration of from_pretrained and from_single_file #10208

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

Conversation

suzukimain
Copy link
Contributor

@suzukimain suzukimain commented Dec 13, 2024

What does this PR do?

This PR integrates the from_single_file function into from_pretrained

pip install -q git+https://github.com/suzukimain/diffusers.git@loading_method_integration

from diffusers import StableDiffusionPipeline

# Load a single file checkpoint
pipe = StableDiffusionPipeline.from_pretrained("https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5/blob/main/v1-5-pruned-emaonly.safetensors")

# Load a multi-folder Diffusers format checkpoint
pipe = StableDiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5")

Additionally, since AutoPipeline doesn't have a method to load single file checkpoints, I've adapted a part of the model_search functionality to enable this capability.

from diffusers import AutoPipelineForText2Image

# Load a single file checkpoint
pipe = AutoPipelineForText2Image.from_pretrained("https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5/blob/main/v1-5-pruned-emaonly.safetensors")

# Load a multi-folder Diffusers format checkpoint
pipe = AutoPipelineForText2Image.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5")

Edit:
Reflected changes

Before submitting

Who can review?

@yiyixuxu ,@asomoza

@suzukimain
Copy link
Contributor Author

This PR allows these issues to be corrected.
#6986 #7496 #7979

@suzukimain
Copy link
Contributor Author

Updated dictionary used when loading the single file checkpoint in AutoPipeline

@suzukimain
Copy link
Contributor Author

Can anyone please review this PR?

@suzukimain
Copy link
Contributor Author

suzukimain commented Dec 23, 2024

Hi, Could you please review this PR if possible?
I hope to not have caused any inconvenience. Thank you for your cooperation.
cc: @hlky @DN6

@bghira
Copy link
Contributor

bghira commented Jan 3, 2025

cc @yiyixuxu @a-r-r-o-w

@DN6
Copy link
Collaborator

DN6 commented Jan 7, 2025

Hi @suzukimain thank you for taking the time to put this together.

I think if we were to unify single file and from pretrained loading it would be by placing all the loading logic under from_pretrained rather than create a new loading method.

We can look into how to unify the two loading methods, but I don't think we can merge this PR in its current state.

cc'ing: @yiyixuxu for her thoughts as well.

local_files_only=local_files_only,
revision=revision,
)
pipeline_class = _get_pipeline_class(cls, class_name=cls.__name__, config=None)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't pass the class_name when loading a single file checkpoint in DiffusionPipeline, it will result in a TypeError.

Comment on lines +752 to +771
# Load the pipeline from a single file
elif load_method_name == "from_single_file":
# The arguments for the __init__ method of `DiffusionPipeline` are keyword arguments, so they cannot be loaded from `from_single_file`
if cls.__name__ == "DiffusionPipeline":
# import it here to avoid circular import
from .stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt

checkpoint = load_single_file_checkpoint(
pretrained_model_name_or_path,
force_download=force_download,
proxies=proxies,
token=token,
cache_dir=cache_dir,
local_files_only=local_files_only,
revision=revision,
)

return download_from_original_stable_diffusion_ckpt(checkpoint, **kwargs)
else:
return cls.from_single_file(pretrained_model_name_or_path, **kwargs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm separating the process because executing DiffusionPipeline.from_single_file results in the following error:

ValueError: Pipeline <class 'diffusers.pipelines.pipeline_utils.DiffusionPipeline'> expected {'kwargs', 'args'}, but only set() were passed.

@suzukimain
Copy link
Contributor Author

Hello, I have made corrections based on your advice.
cc @DN6 @yiyixuxu

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.

3 participants