|
| 1 | +# Copyright 2024 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import math |
| 16 | +from typing import List, Optional, Tuple, Union |
| 17 | + |
| 18 | +import torch |
| 19 | + |
| 20 | +from .guider_utils import GuidanceMixin, _replace_attention_processors, rescale_noise_cfg |
| 21 | + |
| 22 | + |
| 23 | +class PerturbedAttentionGuidance(GuidanceMixin): |
| 24 | + """ |
| 25 | + Perturbed Attention Guidance (PAB): https://huggingface.co/papers/2403.17377 |
| 26 | +
|
| 27 | + Args: |
| 28 | + pag_applied_layers (`str` or `List[str]`): |
| 29 | + The name of the attention layers where Perturbed Attention Guidance is applied. This can be a single layer |
| 30 | + name or a list of layer names. The names should either be FQNs (fully qualified names) to each attention |
| 31 | + layer or a regex pattern that matches the FQNs of the attention layers. For example, if you want to apply |
| 32 | + PAG to transformer blocks 10 and 20, you can set this to `["transformer_blocks.10", |
| 33 | + "transformer_blocks.20"]`, or `"transformer_blocks.(10|20)"`. |
| 34 | + guidance_scale (`float`, defaults to `7.5`): |
| 35 | + The scale parameter for classifier-free guidance. Higher values result in stronger conditioning on the text |
| 36 | + prompt, while lower values allow for more freedom in generation. Higher values may lead to saturation and |
| 37 | + deterioration of image quality. |
| 38 | + pag_scale (`float`, defaults to `3.0`): |
| 39 | + The scale parameter for perturbed attention guidance. |
| 40 | + guidance_rescale (`float`, defaults to `0.0`): |
| 41 | + The rescale factor applied to the noise predictions. This is used to improve image quality and fix |
| 42 | + overexposure. Based on Section 3.4 from [Common Diffusion Noise Schedules and Sample Steps are |
| 43 | + Flawed](https://huggingface.co/papers/2305.08891). |
| 44 | + use_original_formulation (`bool`, defaults to `False`): |
| 45 | + Whether to use the original formulation of classifier-free guidance as proposed in the paper. By default, |
| 46 | + we use the diffusers-native implementation that has been in the codebase for a long time. See |
| 47 | + [~guiders.classifier_free_guidance.ClassifierFreeGuidance] for more details. |
| 48 | + """ |
| 49 | + |
| 50 | + _input_predictions = ["pred_cond", "pred_uncond", "pred_perturbed"] |
| 51 | + |
| 52 | + def __init__( |
| 53 | + self, |
| 54 | + pag_applied_layers: Union[str, List[str]], |
| 55 | + guidance_scale: float = 7.5, |
| 56 | + pag_scale: float = 3.0, |
| 57 | + skip_context_attention: bool = False, |
| 58 | + guidance_rescale: float = 0.0, |
| 59 | + use_original_formulation: bool = False, |
| 60 | + ): |
| 61 | + super().__init__() |
| 62 | + |
| 63 | + self.pag_applied_layers = pag_applied_layers |
| 64 | + self.guidance_scale = guidance_scale |
| 65 | + self.pag_scale = pag_scale |
| 66 | + self.skip_context_attention = skip_context_attention |
| 67 | + self.guidance_rescale = guidance_rescale |
| 68 | + self.use_original_formulation = use_original_formulation |
| 69 | + |
| 70 | + self._is_pag_batch = False |
| 71 | + self._original_processors = None |
| 72 | + self._denoiser = None |
| 73 | + |
| 74 | + def prepare_models(self, denoiser: torch.nn.Module): |
| 75 | + self._denoiser = denoiser |
| 76 | + |
| 77 | + def prepare_inputs(self, *args: Union[Tuple[torch.Tensor], List[torch.Tensor]]) -> Tuple[List[torch.Tensor], ...]: |
| 78 | + num_conditions = self.num_conditions |
| 79 | + list_of_inputs = [] |
| 80 | + for arg in args: |
| 81 | + if isinstance(arg, torch.Tensor): |
| 82 | + list_of_inputs.append([arg] * num_conditions) |
| 83 | + elif isinstance(arg, (tuple, list)): |
| 84 | + if len(arg) != 2: |
| 85 | + raise ValueError( |
| 86 | + f"Expected a tuple or list of length 2, but got {len(arg)} for argument {arg}. Please provide a tuple/list of length 2 " |
| 87 | + f"with the first element being the conditional input and the second element being the unconditional input or None." |
| 88 | + ) |
| 89 | + if arg[1] is None: |
| 90 | + # Only conditioning inputs for all batches |
| 91 | + list_of_inputs.append([arg[0]] * num_conditions) |
| 92 | + else: |
| 93 | + list_of_inputs.append([arg[0], arg[1], arg[0]]) |
| 94 | + else: |
| 95 | + raise ValueError( |
| 96 | + f"Expected a tensor, tuple, or list, but got {type(arg)} for argument {arg}. Please provide a tensor, tuple, or list." |
| 97 | + ) |
| 98 | + return tuple(list_of_inputs) |
| 99 | + |
| 100 | + def prepare_outputs(self, pred: torch.Tensor) -> None: |
| 101 | + self._num_outputs_prepared += 1 |
| 102 | + if self._num_outputs_prepared > self.num_conditions: |
| 103 | + raise ValueError(f"Expected {self.num_conditions} outputs, but prepare_outputs called more times.") |
| 104 | + key = self._input_predictions[self._num_outputs_prepared - 1] |
| 105 | + if not self._is_cfg_enabled() and self._is_pag_enabled(): |
| 106 | + # If we're predicting pred_cond and pred_perturbed only, we need to set the key to pred_perturbed |
| 107 | + # to avoid writing into pred_uncond which is not used |
| 108 | + if self._num_outputs_prepared == 2: |
| 109 | + key = "pred_perturbed" |
| 110 | + self._preds[key] = pred |
| 111 | + |
| 112 | + # Prepare denoiser for perturbed attention prediction if needed |
| 113 | + if not self._is_pag_enabled(): |
| 114 | + return |
| 115 | + should_register_pag = (self._is_cfg_enabled() and self._num_outputs_prepared == 2) or ( |
| 116 | + not self._is_cfg_enabled() and self._num_outputs_prepared == 1 |
| 117 | + ) |
| 118 | + if should_register_pag: |
| 119 | + self._is_pag_batch = True |
| 120 | + self._original_processors = _replace_attention_processors( |
| 121 | + self._denoiser, |
| 122 | + self.pag_applied_layers, |
| 123 | + skip_context_attention=self.skip_context_attention, |
| 124 | + metadata_name="perturbed_attention_guidance_processor_cls", |
| 125 | + ) |
| 126 | + elif self._is_pag_batch: |
| 127 | + # Restore the original attention processors |
| 128 | + _replace_attention_processors(self._denoiser, processors=self._original_processors) |
| 129 | + self._is_pag_batch = False |
| 130 | + self._original_processors = None |
| 131 | + |
| 132 | + def cleanup_models(self, denoiser: torch.nn.Module): |
| 133 | + self._denoiser = None |
| 134 | + |
| 135 | + def forward( |
| 136 | + self, |
| 137 | + pred_cond: torch.Tensor, |
| 138 | + pred_uncond: Optional[torch.Tensor] = None, |
| 139 | + pred_perturbed: Optional[torch.Tensor] = None, |
| 140 | + ) -> torch.Tensor: |
| 141 | + pred = None |
| 142 | + |
| 143 | + if not self._is_cfg_enabled() and not self._is_pag_enabled(): |
| 144 | + pred = pred_cond |
| 145 | + elif not self._is_cfg_enabled(): |
| 146 | + shift = pred_cond - pred_perturbed |
| 147 | + pred = pred_cond + self.pag_scale * shift |
| 148 | + elif not self._is_pag_enabled(): |
| 149 | + shift = pred_cond - pred_uncond |
| 150 | + pred = pred_cond if self.use_original_formulation else pred_uncond |
| 151 | + pred = pred + self.guidance_scale * shift |
| 152 | + else: |
| 153 | + shift = pred_cond - pred_uncond |
| 154 | + shift_perturbed = pred_cond - pred_perturbed |
| 155 | + pred = pred_cond if self.use_original_formulation else pred_uncond |
| 156 | + pred = pred + self.guidance_scale * shift + self.pag_scale * shift_perturbed |
| 157 | + |
| 158 | + if self.guidance_rescale > 0.0: |
| 159 | + pred = rescale_noise_cfg(pred, pred_cond, self.guidance_rescale) |
| 160 | + |
| 161 | + return pred |
| 162 | + |
| 163 | + @property |
| 164 | + def num_conditions(self) -> int: |
| 165 | + num_conditions = 1 |
| 166 | + if self._is_cfg_enabled(): |
| 167 | + num_conditions += 1 |
| 168 | + if self._is_pag_enabled(): |
| 169 | + num_conditions += 1 |
| 170 | + return num_conditions |
| 171 | + |
| 172 | + def _is_cfg_enabled(self) -> bool: |
| 173 | + if self.use_original_formulation: |
| 174 | + return not math.isclose(self.guidance_scale, 0.0) |
| 175 | + else: |
| 176 | + return not math.isclose(self.guidance_scale, 1.0) |
| 177 | + |
| 178 | + def _is_pag_enabled(self) -> bool: |
| 179 | + is_zero = math.isclose(self.pag_scale, 0.0) |
| 180 | + return not is_zero |
0 commit comments