|
| 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 Optional, Union, Tuple, List |
| 17 | + |
| 18 | +import torch |
| 19 | + |
| 20 | +from .guider_utils import BaseGuidance, rescale_noise_cfg, _default_prepare_inputs |
| 21 | + |
| 22 | + |
| 23 | +class ClassifierFreeZeroStarGuidance(BaseGuidance): |
| 24 | + """ |
| 25 | + Classifier-free Zero* (CFG-Zero*): https://huggingface.co/papers/2503.18886 |
| 26 | + |
| 27 | + This is an implementation of the Classifier-Free Zero* guidance technique, which is a variant of classifier-free |
| 28 | + guidance. It proposes zero initialization of the noise predictions for the first few steps of the diffusion |
| 29 | + process, and also introduces an optimal rescaling factor for the noise predictions, which can help in improving the |
| 30 | + quality of generated images. |
| 31 | + |
| 32 | + The authors of the paper suggest setting zero initialization in the first 4% of the inference steps. |
| 33 | + |
| 34 | + Args: |
| 35 | + guidance_scale (`float`, defaults to `7.5`): |
| 36 | + The scale parameter for classifier-free guidance. Higher values result in stronger conditioning on the text |
| 37 | + prompt, while lower values allow for more freedom in generation. Higher values may lead to saturation and |
| 38 | + deterioration of image quality. |
| 39 | + zero_init_steps (`int`, defaults to `1`): |
| 40 | + The number of inference steps for which the noise predictions are zeroed out (see Section 4.2). |
| 41 | + guidance_rescale (`float`, defaults to `0.0`): |
| 42 | + The rescale factor applied to the noise predictions. This is used to improve image quality and fix |
| 43 | + overexposure. Based on Section 3.4 from [Common Diffusion Noise Schedules and Sample Steps are |
| 44 | + Flawed](https://huggingface.co/papers/2305.08891). |
| 45 | + use_original_formulation (`bool`, defaults to `False`): |
| 46 | + Whether to use the original formulation of classifier-free guidance as proposed in the paper. By default, |
| 47 | + we use the diffusers-native implementation that has been in the codebase for a long time. See |
| 48 | + [~guiders.classifier_free_guidance.ClassifierFreeGuidance] for more details. |
| 49 | + start (`float`, defaults to `0.01`): |
| 50 | + The fraction of the total number of denoising steps after which guidance starts. |
| 51 | + stop (`float`, defaults to `0.2`): |
| 52 | + The fraction of the total number of denoising steps after which guidance stops. |
| 53 | + """ |
| 54 | + |
| 55 | + _input_predictions = ["pred_cond", "pred_uncond"] |
| 56 | + |
| 57 | + def __init__( |
| 58 | + self, |
| 59 | + guidance_scale: float = 7.5, |
| 60 | + zero_init_steps: int = 1, |
| 61 | + guidance_rescale: float = 0.0, |
| 62 | + use_original_formulation: bool = False, |
| 63 | + start: float = 0.0, |
| 64 | + stop: float = 1.0, |
| 65 | + ): |
| 66 | + super().__init__(start, stop) |
| 67 | + |
| 68 | + self.guidance_scale = guidance_scale |
| 69 | + self.zero_init_steps = zero_init_steps |
| 70 | + self.guidance_rescale = guidance_rescale |
| 71 | + self.use_original_formulation = use_original_formulation |
| 72 | + |
| 73 | + def prepare_inputs(self, denoiser: torch.nn.Module, *args: Union[Tuple[torch.Tensor], List[torch.Tensor]]) -> Tuple[List[torch.Tensor], ...]: |
| 74 | + return _default_prepare_inputs(denoiser, self.num_conditions, *args) |
| 75 | + |
| 76 | + def prepare_outputs(self, denoiser: torch.nn.Module, pred: torch.Tensor) -> None: |
| 77 | + self._num_outputs_prepared += 1 |
| 78 | + if self._num_outputs_prepared > self.num_conditions: |
| 79 | + raise ValueError(f"Expected {self.num_conditions} outputs, but prepare_outputs called more times.") |
| 80 | + key = self._input_predictions[self._num_outputs_prepared - 1] |
| 81 | + self._preds[key] = pred |
| 82 | + |
| 83 | + def forward(self, pred_cond: torch.Tensor, pred_uncond: Optional[torch.Tensor] = None) -> torch.Tensor: |
| 84 | + pred = None |
| 85 | + |
| 86 | + if self._step < self.zero_init_steps: |
| 87 | + pred = torch.zeros_like(pred_cond) |
| 88 | + elif not self._is_cfg_enabled(): |
| 89 | + pred = pred_cond |
| 90 | + else: |
| 91 | + pred_cond_flat = pred_cond.flatten(1) |
| 92 | + pred_uncond_flat = pred_uncond.flatten(1) |
| 93 | + alpha = cfg_zero_star_scale(pred_cond_flat, pred_uncond_flat) |
| 94 | + alpha = alpha.view(-1, *(1,) * (len(pred_cond.shape) - 1)) |
| 95 | + pred_uncond = pred_uncond * alpha |
| 96 | + shift = pred_cond - pred_uncond |
| 97 | + pred = pred_cond if self.use_original_formulation else pred_uncond |
| 98 | + pred = pred + self.guidance_scale * shift |
| 99 | + |
| 100 | + if self.guidance_rescale > 0.0: |
| 101 | + pred = rescale_noise_cfg(pred, pred_cond, self.guidance_rescale) |
| 102 | + |
| 103 | + return pred |
| 104 | + |
| 105 | + @property |
| 106 | + def is_conditional(self) -> bool: |
| 107 | + return self._num_outputs_prepared == 0 |
| 108 | + |
| 109 | + @property |
| 110 | + def num_conditions(self) -> int: |
| 111 | + num_conditions = 1 |
| 112 | + if self._is_cfg_enabled(): |
| 113 | + num_conditions += 1 |
| 114 | + return num_conditions |
| 115 | + |
| 116 | + def _is_cfg_enabled(self) -> bool: |
| 117 | + if not self._enabled: |
| 118 | + return False |
| 119 | + |
| 120 | + is_within_range = True |
| 121 | + if self._num_inference_steps is not None: |
| 122 | + skip_start_step = int(self._start * self._num_inference_steps) |
| 123 | + skip_stop_step = int(self._stop * self._num_inference_steps) |
| 124 | + is_within_range = skip_start_step <= self._step < skip_stop_step |
| 125 | + |
| 126 | + is_close = False |
| 127 | + if self.use_original_formulation: |
| 128 | + is_close = math.isclose(self.guidance_scale, 0.0) |
| 129 | + else: |
| 130 | + is_close = math.isclose(self.guidance_scale, 1.0) |
| 131 | + |
| 132 | + return is_within_range and not is_close |
| 133 | + |
| 134 | + |
| 135 | +def cfg_zero_star_scale(cond: torch.Tensor, uncond: torch.Tensor, eps: float = 1e-8) -> torch.Tensor: |
| 136 | + cond_dtype = cond.dtype |
| 137 | + cond = cond.float() |
| 138 | + uncond = uncond.float() |
| 139 | + dot_product = torch.sum(cond * uncond, dim=1, keepdim=True) |
| 140 | + squared_norm = torch.sum(uncond**2, dim=1, keepdim=True) + eps |
| 141 | + # st_star = v_cond^T * v_uncond / ||v_uncond||^2 |
| 142 | + scale = dot_product / squared_norm |
| 143 | + return scale.to(dtype=cond_dtype) |
0 commit comments