@@ -698,11 +698,6 @@ def __call__(
698698 lora_scale = text_encoder_lora_scale ,
699699 clip_skip = clip_skip ,
700700 )
701- # For classifier free guidance, we need to do two forward passes.
702- # Here we concatenate the unconditional and text embeddings into a single batch
703- # to avoid doing two forward passes
704- if do_classifier_free_guidance :
705- prompt_embeds = torch .cat ([negative_prompt_embeds , prompt_embeds ])
706701
707702 # 4. Prepare timesteps
708703 self .scheduler .set_timesteps (num_inference_steps , device = device )
@@ -733,11 +728,20 @@ def __call__(
733728 latent_model_input = torch .cat ([latents ] * 2 ) if do_classifier_free_guidance else latents
734729 latent_model_input = self .scheduler .scale_model_input (latent_model_input , t )
735730
731+ # For classifier free guidance, we need to do two forward passes.
732+ # Here we concatenate the unconditional and text embeddings into a single batch
733+ # to avoid doing two forward passes
734+ prompt_embeds_cond = (
735+ torch .cat ([negative_prompt_embeds , prompt_embeds ])
736+ if do_classifier_free_guidance
737+ else prompt_embeds
738+ )
739+
736740 # predict the noise residual
737741 noise_pred = self .unet (
738742 latent_model_input ,
739743 t ,
740- encoder_hidden_states = prompt_embeds ,
744+ encoder_hidden_states = prompt_embeds_cond ,
741745 cross_attention_kwargs = cross_attention_kwargs ,
742746 return_dict = False ,
743747 )[0 ]
@@ -758,13 +762,14 @@ def __call__(
758762 callback_kwargs = {}
759763 for k in callback_on_step_end_inputs :
760764 callback_kwargs [k ] = locals ()[k ]
761- callback_kwargs = callback_on_step_end (i , t , callback_kwargs )
762-
763- latents = callback_kwargs .pop ("latents" , latents )
764- guidance_scale = callback_kwargs .pop ("guidance_scale" , guidance_scale )
765- prompt_embeds = callback_kwargs .pop ("prompt_embeds" , prompt_embeds )
766- cross_attention_kwargs = callback_kwargs .pop ("cross_attention_kwargs" , cross_attention_kwargs )
767- guidance_rescale = callback_kwargs .pop ("guidance_rescale" , guidance_rescale )
765+ callback_results = callback_on_step_end (i , t , callback_kwargs )
766+
767+ latents = callback_results .pop ("latents" , latents )
768+ guidance_scale = callback_results .pop ("guidance_scale" , guidance_scale )
769+ prompt_embeds = callback_results .pop ("prompt_embeds" , prompt_embeds )
770+ negative_prompt_embeds = callback_results .pop ("negative_prompt_embeds" , negative_prompt_embeds )
771+ cross_attention_kwargs = callback_results .pop ("cross_attention_kwargs" , cross_attention_kwargs )
772+ guidance_rescale = callback_results .pop ("guidance_rescale" , guidance_rescale )
768773 do_classifier_free_guidance = guidance_scale > 1.0
769774
770775 # call the callback, if provided
0 commit comments