-
Couldn't load subscription status.
- Fork 6.5k
add skip_layers argument to SD3 transformer model class #9880
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
Changes from 6 commits
21075fa
fd4a229
103536f
b87a597
2583726
791d5af
481591a
092c358
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -694,6 +694,10 @@ def __call__( | |||
| callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None, | ||||
| callback_on_step_end_tensor_inputs: List[str] = ["latents"], | ||||
| max_sequence_length: int = 256, | ||||
| skip_guidance_layers: List[int] = None, | ||||
| skip_layer_guidance_scale: int = 2.8, | ||||
| skip_layer_guidance_stop: int = 0.2, | ||||
| skip_layer_guidance_start: int = 0.01, | ||||
| ): | ||||
| r""" | ||||
| Function invoked when calling the pipeline for generation. | ||||
|
|
@@ -778,6 +782,22 @@ def __call__( | |||
| will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the | ||||
| `._callback_tensor_inputs` attribute of your pipeline class. | ||||
| max_sequence_length (`int` defaults to 256): Maximum sequence length to use with the `prompt`. | ||||
| skip_guidance_layers (`List[int]`, *optional*): A list of integers that specify layers to skip during guidance. | ||||
| If not provided, all layers will be used for guidance. If provided, the guidance will only be applied | ||||
| to the layers specified in the list. Recommended value by StabiltyAI for Stable Diffusion 3.5 Medium is | ||||
| [7, 8, 9]. | ||||
| skip_layer_guidance_scale (`int`, *optional*): The scale of the guidance for the layers specified in | ||||
| `skip_guidance_layers`. The guidance will be applied to the layers specified in `skip_guidance_layers` | ||||
| with a scale of `skip_layer_guidance_scale`. The guidance will be applied to the rest of the layers with | ||||
| a scale of `1`. | ||||
| skip_layer_guidance_stop (`int`, *optional*): The step at which the guidance for the layers specified in | ||||
| `skip_guidance_layers` will stop. The guidance will be applied to the layers specified in | ||||
| `skip_guidance_layers` until the fraction specified in `skip_layer_guidance_stop`. Recommended value by | ||||
| StabiltyAI for Stable Diffusion 3.5 Medium is 0.2. | ||||
| skip_layer_guidance_start (`int`, *optional*): The step at which the guidance for the layers specified in | ||||
| `skip_guidance_layers` will start. The guidance will be applied to the layers specified in | ||||
| `skip_guidance_layers` from the fraction specified in `skip_layer_guidance_start`. Recommended value by | ||||
| StabiltyAI for Stable Diffusion 3.5 Medium is 0.01. | ||||
|
|
||||
| Examples: | ||||
|
|
||||
|
|
@@ -809,6 +829,7 @@ def __call__( | |||
| ) | ||||
|
|
||||
| self._guidance_scale = guidance_scale | ||||
| self._skip_layer_guidance_scale = skip_layer_guidance_scale | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to add a decorator for this too, like this diffusers/src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py Line 642 in 07d0fbf
|
||||
| self._clip_skip = clip_skip | ||||
| self._joint_attention_kwargs = joint_attention_kwargs | ||||
| self._interrupt = False | ||||
|
|
@@ -851,6 +872,9 @@ def __call__( | |||
| ) | ||||
|
|
||||
| if self.do_classifier_free_guidance: | ||||
| if skip_guidance_layers is not None: | ||||
| original_prompt_embeds = prompt_embeds | ||||
| original_pooled_prompt_embeds = pooled_prompt_embeds | ||||
| prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0) | ||||
| pooled_prompt_embeds = torch.cat([negative_pooled_prompt_embeds, pooled_prompt_embeds], dim=0) | ||||
|
|
||||
|
|
@@ -879,7 +903,7 @@ def __call__( | |||
| continue | ||||
|
|
||||
| # expand the latents if we are doing classifier free guidance | ||||
| latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents | ||||
| latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance and skip_guidance_layers is None else latents | ||||
| # broadcast to batch dimension in a way that's compatible with ONNX/Core ML | ||||
| timestep = t.expand(latent_model_input.shape[0]) | ||||
|
|
||||
|
|
@@ -896,6 +920,18 @@ def __call__( | |||
| if self.do_classifier_free_guidance: | ||||
| noise_pred_uncond, noise_pred_text = noise_pred.chunk(2) | ||||
| noise_pred = noise_pred_uncond + self.guidance_scale * (noise_pred_text - noise_pred_uncond) | ||||
| should_skip_layers = True if i > num_inference_steps * skip_layer_guidance_start and i < num_inference_steps* skip_layer_guidance_stop else False | ||||
| if skip_guidance_layers is not None and should_skip_layers: | ||||
yiyixuxu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| noise_pred_skip_layers = self.transformer( | ||||
| hidden_states=latent_model_input, | ||||
| timestep=timestep, | ||||
| encoder_hidden_states=original_prompt_embeds, | ||||
| pooled_projections=original_pooled_prompt_embeds, | ||||
| joint_attention_kwargs=self.joint_attention_kwargs, | ||||
| return_dict=False, | ||||
| skip_layers=skip_guidance_layers, | ||||
| )[0] | ||||
| noise_pred = noise_pred + (noise_pred_text - noise_pred_skip_layers) * self._skip_layer_guidance_scale | ||||
|
|
||||
| # compute the previous noisy sample x_t -> x_t-1 | ||||
| latents_dtype = latents.dtype | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we skip the block of code that needs to be skipped instead of adding duplicated code here
otherwise, if we have to change this part of the code that handles controlnet residual in the future, we have to remember to change both places, which is not great