2727class ClassifierFreeGuidance (BaseGuidance ):
2828 """
2929 Classifier-free guidance (CFG): https://huggingface.co/papers/2207.12598
30-
30+
3131 CFG is a technique used to improve generation quality and condition-following in diffusion models. It works by
3232 jointly training a model on both conditional and unconditional data, and using a weighted sum of the two during
33- inference. This allows the model to tradeoff between generation quality and sample diversity.
34- The original paper proposes scaling and shifting the conditional distribution based on the difference between
35- conditional and unconditional predictions. [x_pred = x_cond + scale * (x_cond - x_uncond)]
36-
33+ inference. This allows the model to tradeoff between generation quality and sample diversity. The original paper
34+ proposes scaling and shifting the conditional distribution based on the difference between conditional and
35+ unconditional predictions. [x_pred = x_cond + scale * (x_cond - x_uncond)]
36+
3737 Diffusers implemented the scaling and shifting on the unconditional prediction instead based on the [Imagen
3838 paper](https://huggingface.co/papers/2205.11487), which is equivalent to what the original paper proposed in
3939 theory. [x_pred = x_uncond + scale * (x_cond - x_uncond)]
40-
40+
4141 The intution behind the original formulation can be thought of as moving the conditional distribution estimates
4242 further away from the unconditional distribution estimates, while the diffusers-native implementation can be
4343 thought of as moving the unconditional distribution towards the conditional distribution estimates to get rid of
4444 the unconditional predictions (usually negative features like "bad quality, bad anotomy, watermarks", etc.)
45-
45+
4646 The `use_original_formulation` argument can be set to `True` to use the original CFG formulation mentioned in the
4747 paper. By default, we use the diffusers-native implementation that has been in the codebase for a long time.
48-
48+
4949 Args:
5050 guidance_scale (`float`, defaults to `7.5`):
5151 The scale parameter for classifier-free guidance. Higher values result in stronger conditioning on the text
@@ -68,16 +68,22 @@ class ClassifierFreeGuidance(BaseGuidance):
6868 _input_predictions = ["pred_cond" , "pred_uncond" ]
6969
7070 def __init__ (
71- self , guidance_scale : float = 7.5 , guidance_rescale : float = 0.0 , use_original_formulation : bool = False , start : float = 0.0 , stop : float = 1.0
71+ self ,
72+ guidance_scale : float = 7.5 ,
73+ guidance_rescale : float = 0.0 ,
74+ use_original_formulation : bool = False ,
75+ start : float = 0.0 ,
76+ stop : float = 1.0 ,
7277 ):
7378 super ().__init__ (start , stop )
7479
7580 self .guidance_scale = guidance_scale
7681 self .guidance_rescale = guidance_rescale
7782 self .use_original_formulation = use_original_formulation
7883
79- def prepare_inputs (self , data : "BlockState" , input_fields : Optional [Dict [str , Union [str , Tuple [str , str ]]]] = None ) -> List ["BlockState" ]:
80-
84+ def prepare_inputs (
85+ self , data : "BlockState" , input_fields : Optional [Dict [str , Union [str , Tuple [str , str ]]]] = None
86+ ) -> List ["BlockState" ]:
8187 if input_fields is None :
8288 input_fields = self ._input_fields
8389
0 commit comments