-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
Merged
asomoza
merged 8 commits into
huggingface:main
from
bghira:feature-sd3/skip-layer-guidance
Nov 19, 2024
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21075fa
add skip_layers argument to SD3 transformer model class
fd4a229
add unit test for skip_layers in stable diffusion 3
103536f
sd3: pipeline should support skip layer guidance
b87a597
Merge branch 'main' into feature-sd3/skip-layer-guidance
bghira 2583726
Merge branch 'main' into feature-sd3/skip-layer-guidance
bghira 791d5af
Merge branch 'main' into feature-sd3/skip-layer-guidance
bghira 481591a
up
yiyixuxu 092c358
Merge branch 'main' into feature-sd3/skip-layer-guidance
asomoza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| 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 | ||||
|
Collaborator
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 | ||||
|
|
||||
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
Oops, something went wrong.
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.
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