Skip to content

Commit a1cb106

Browse files
erlidingsayakpaul
andauthored
instruct pix2pix pipeline: remove sigma scaling when computing classifier free guidance (#7006)
remove sigma scaling when computing cfg Co-authored-by: Sayak Paul <[email protected]>
1 parent 5dd8e04 commit a1cb106

File tree

2 files changed

+0
-40
lines changed

2 files changed

+0
-40
lines changed

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,6 @@ def __call__(
323323
batch_size = prompt_embeds.shape[0]
324324

325325
device = self._execution_device
326-
# check if scheduler is in sigmas space
327-
scheduler_is_in_sigma_space = hasattr(self.scheduler, "sigmas")
328326

329327
# 2. Encode input prompt
330328
prompt_embeds = self._encode_prompt(
@@ -411,15 +409,6 @@ def __call__(
411409
return_dict=False,
412410
)[0]
413411

414-
# Hack:
415-
# For karras style schedulers the model does classifer free guidance using the
416-
# predicted_original_sample instead of the noise_pred. So we need to compute the
417-
# predicted_original_sample here if we are using a karras style scheduler.
418-
if scheduler_is_in_sigma_space:
419-
step_index = (self.scheduler.timesteps == t).nonzero()[0].item()
420-
sigma = self.scheduler.sigmas[step_index]
421-
noise_pred = latent_model_input - sigma * noise_pred
422-
423412
# perform guidance
424413
if self.do_classifier_free_guidance:
425414
noise_pred_text, noise_pred_image, noise_pred_uncond = noise_pred.chunk(3)
@@ -429,15 +418,6 @@ def __call__(
429418
+ self.image_guidance_scale * (noise_pred_image - noise_pred_uncond)
430419
)
431420

432-
# Hack:
433-
# For karras style schedulers the model does classifer free guidance using the
434-
# predicted_original_sample instead of the noise_pred. But the scheduler.step function
435-
# expects the noise_pred and computes the predicted_original_sample internally. So we
436-
# need to overwrite the noise_pred here such that the value of the computed
437-
# predicted_original_sample is correct.
438-
if scheduler_is_in_sigma_space:
439-
noise_pred = (noise_pred - latents) / (-sigma)
440-
441421
# compute the previous noisy sample x_t -> x_t-1
442422
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
443423

src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,6 @@ def __call__(
774774
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
775775
# corresponds to doing no classifier free guidance.
776776
do_classifier_free_guidance = guidance_scale > 1.0 and image_guidance_scale >= 1.0
777-
# check if scheduler is in sigmas space
778-
scheduler_is_in_sigma_space = hasattr(self.scheduler, "sigmas")
779777

780778
# 3. Encode input prompt
781779
text_encoder_lora_scale = (
@@ -906,15 +904,6 @@ def __call__(
906904
return_dict=False,
907905
)[0]
908906

909-
# Hack:
910-
# For karras style schedulers the model does classifer free guidance using the
911-
# predicted_original_sample instead of the noise_pred. So we need to compute the
912-
# predicted_original_sample here if we are using a karras style scheduler.
913-
if scheduler_is_in_sigma_space:
914-
step_index = (self.scheduler.timesteps == t).nonzero()[0].item()
915-
sigma = self.scheduler.sigmas[step_index]
916-
noise_pred = latent_model_input - sigma * noise_pred
917-
918907
# perform guidance
919908
if do_classifier_free_guidance:
920909
noise_pred_text, noise_pred_image, noise_pred_uncond = noise_pred.chunk(3)
@@ -928,15 +917,6 @@ def __call__(
928917
# Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
929918
noise_pred = rescale_noise_cfg(noise_pred, noise_pred_text, guidance_rescale=guidance_rescale)
930919

931-
# Hack:
932-
# For karras style schedulers the model does classifer free guidance using the
933-
# predicted_original_sample instead of the noise_pred. But the scheduler.step function
934-
# expects the noise_pred and computes the predicted_original_sample internally. So we
935-
# need to overwrite the noise_pred here such that the value of the computed
936-
# predicted_original_sample is correct.
937-
if scheduler_is_in_sigma_space:
938-
noise_pred = (noise_pred - latents) / (-sigma)
939-
940920
# compute the previous noisy sample x_t -> x_t-1
941921
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
942922

0 commit comments

Comments
 (0)