Skip to content

Commit 7bbc036

Browse files
huggingface#3487 Fix inpainting strength for various samplers (huggingface#3532)
* Throw error if strength adjusted num_inference_steps < 1 * Added new fast test to check ValueError raised when num_inference_steps < 1 when strength adjusts the num_inference_steps then the inpainting pipeline should fail * fix huggingface#3487 initial latents are now only scaled by init_noise_sigma when pure noise updated this commit w.r.t the latest merge here: huggingface#3533 * fix --------- Co-authored-by: Patrick von Platen <[email protected]>
1 parent 612a312 commit 7bbc036

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

pipelines/controlnet/pipeline_controlnet_inpaint.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,12 +863,13 @@ def prepare_latents(
863863

864864
if latents is None:
865865
noise = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
866+
# if strength is 1. then initialise the latents to noise, else initial to image + noise
866867
latents = noise if is_strength_max else self.scheduler.add_noise(image_latents, noise, timestep)
868+
# if pure noise then scale the initial latents by the Scheduler's init sigma
869+
latents = latents * self.scheduler.init_noise_sigma if is_strength_max else latents
867870
else:
868871
latents = latents.to(device)
869-
870-
# scale the initial noise by the standard deviation required by the scheduler
871-
latents = latents * self.scheduler.init_noise_sigma
872+
latents = latents * self.scheduler.init_noise_sigma
872873

873874
outputs = (latents,)
874875

pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,13 @@ def prepare_latents(
648648

649649
if latents is None:
650650
noise = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
651+
# if strength is 1. then initialise the latents to noise, else initial to image + noise
651652
latents = noise if is_strength_max else self.scheduler.add_noise(image_latents, noise, timestep)
653+
# if pure noise then scale the initial latents by the Scheduler's init sigma
654+
latents = latents * self.scheduler.init_noise_sigma if is_strength_max else latents
652655
else:
653656
latents = latents.to(device)
654-
655-
# scale the initial noise by the standard deviation required by the scheduler
656-
latents = latents * self.scheduler.init_noise_sigma
657+
latents = latents * self.scheduler.init_noise_sigma
657658

658659
outputs = (latents,)
659660

@@ -912,6 +913,12 @@ def __call__(
912913
timesteps, num_inference_steps = self.get_timesteps(
913914
num_inference_steps=num_inference_steps, strength=strength, device=device
914915
)
916+
# check that number of inference steps is not < 1 - as this doesn't make sense
917+
if num_inference_steps < 1:
918+
raise ValueError(
919+
f"After adjusting the num_inference_steps by strength parameter: {strength}, the number of pipeline"
920+
f"steps is {num_inference_steps} which is < 1 and not appropriate for this pipeline."
921+
)
915922
# at which timestep to set the initial noise (n.b. 50% if strength is 0.5)
916923
latent_timestep = timesteps[:1].repeat(batch_size * num_images_per_prompt)
917924
# create a boolean to check if the strength is set to 1. if so then initialise the latents with pure noise

0 commit comments

Comments
 (0)