-
Notifications
You must be signed in to change notification settings - Fork 6.5k
refactor get_timesteps for SDXL img2img + add set_begin_index
#9375
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
Merged
+86
−60
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Collaborator
Author
|
slow test 1, tested for # branch = "main" # or "add-begin-index-sdxlimg2img"
branch = "add-begin-index-sdxlimg2img"
# output_type ="pil" # or "pt"
output_type = "pt"
steps = 20
seed = 0
denoising_start = 0.8 # or None
# denoising_start = None
import torch
from diffusers import DiffusionPipeline
from diffusers import DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler
import os
from diffusers.utils import make_image_grid
import gc
def flush():
"""Wipes off memory."""
gc.collect()
torch.cuda.empty_cache()
torch.cuda.reset_max_memory_allocated()
torch.cuda.reset_peak_memory_stats()
# define all scheduler configs we want to test
config_min = {"final_sigmas_type":"sigma_min"}
config_min_euler = {"final_sigmas_type":"sigma_min", "euler_at_final": True }
config_zero = {"final_sigmas_type":"zero"}
schedulers = {
"DPMPP_2M": {
"min": (DPMSolverMultistepScheduler, config_min),
"min_euler": (DPMSolverMultistepScheduler, config_min_euler),
"zero": (DPMSolverMultistepScheduler, config_zero),
},
"DPMPP_2M_K": {
"min": (DPMSolverMultistepScheduler, {"use_karras_sigmas": True, **config_min}),
"min_euler": (DPMSolverMultistepScheduler, {"use_karras_sigmas": True, **config_min_euler}),
"zero": (DPMSolverMultistepScheduler, {"use_karras_sigmas": True, **config_zero}),
},
"DPMPP_2M_SDE": {
"min": (DPMSolverMultistepScheduler, {"algorithm_type": "sde-dpmsolver++", **config_min}),
"min_euler": (DPMSolverMultistepScheduler, {"algorithm_type": "sde-dpmsolver++", **config_min_euler}),
"zero": (DPMSolverMultistepScheduler, {"algorithm_type": "sde-dpmsolver++", **config_zero}),
},
"DPMPP_2M_SDE_K": {
"min": (DPMSolverMultistepScheduler, {"algorithm_type": "sde-dpmsolver++", "use_karras_sigmas": True, **config_min}),
"min_euler": (DPMSolverMultistepScheduler, {"algorithm_type": "sde-dpmsolver++", "use_karras_sigmas": True, **config_min_euler}),
"zero": (DPMSolverMultistepScheduler, {"use_karras_sigmas": True, "algorithm_type": "sde-dpmsolver++", **config_zero}),
},
"DPMPP": {
"min": (DPMSolverSinglestepScheduler, config_min),
"min_euler": (DPMSolverSinglestepScheduler, config_min_euler),
"zero": (DPMSolverSinglestepScheduler, config_zero),
},
"DPMPP_K": {
"min": (DPMSolverSinglestepScheduler, {"use_karras_sigmas": True, **config_min}),
"min_euler": (DPMSolverSinglestepScheduler, {"use_karras_sigmas": True, **config_min_euler}),
"zero": (DPMSolverSinglestepScheduler, {"use_karras_sigmas": True, **config_zero}),
},
}
# define save directory
save_dir = './yiyi_test_3_output'
if not os.path.exists(save_dir):
os.mkdir(save_dir)
# load base model and create latent
from diffusers import DiffusionPipeline
import torch
base = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16"
).to("cuda")
prompt = "A majestic lion jumping from a big stone at night"
generator = torch.Generator(device='cuda').manual_seed(seed)
image = base(
prompt=prompt,
num_inference_steps=40,
denoising_end=denoising_start,
generator=generator,
output_type="latent",
).images
# load refiner pipe
model_id = "stabilityai/stable-diffusion-xl-refiner-1.0"
pipe = DiffusionPipeline.from_pretrained(
model_id,
text_encoder_2=base.text_encoder_2,
vae=base.vae,
torch_dtype=torch.float16,
variant="fp16",
).to("cuda")
del base
flush()
params = {
"prompt": [prompt],
"num_inference_steps": steps,
"guidance_scale": 7,
"image": image,
"denoising_start":denoising_start,
}
for scheduler_name in schedulers.keys():
scheduler_configs = schedulers[scheduler_name]
for scheduler_config_name in scheduler_configs.keys():
generator = torch.Generator(device='cuda').manual_seed(seed)
scheduler = scheduler_configs[scheduler_config_name][0].from_pretrained(
model_id,
subfolder="scheduler",
**scheduler_configs[scheduler_config_name][1],
)
pipe.scheduler = scheduler
img = pipe(**params, generator=generator, output_type=output_type).images[0]
if output_type == "pt":
if branch == "main":
torch.save(img, os.path.join(save_dir, f"{branch}_{scheduler_name}_{scheduler_config_name}_{steps}_{denoising_start}.pt"))
else:
img_expected = torch.load(os.path.join(save_dir, f"main_{scheduler_name}_{scheduler_config_name}_{steps}_{denoising_start}.pt"))
assert (img - img_expected).abs().max() < 1e-3, f"Image mismatch for {scheduler_name}_{scheduler_config_name}_{steps}_{denoising_start}"
else:
img.save(os.path.join(save_dir, f"{branch}_{scheduler_name}_{scheduler_config_name}_{steps}_{denoising_start}.png")) |
Collaborator
Author
…diffusers into add-begin-index-sdxlimg2img
Collaborator
Author
|
cc @tolgacangoz in case you're interested in giving this a review! (no worries if not) |
sayakpaul
pushed a commit
that referenced
this pull request
Dec 23, 2024
* refator + add begin_index * add kolors img2img to doc
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.












fix #9366
this is a known issue that happens when the first timestep is a duplicated one, so I added
set_begin_indexso we don't rely on the search method to find the first timestep (see more details on #6728)I also refactored the
get_timestepsmethod for SDXL img2img, so it's a little bit easier to reason abouta simple unit test for the
get_timesteps, I will run more slow tests for affected pipelines