
    +h                         S SK r S SKJrJrJrJrJrJr  S SKrSSK	J
r
  SSKJrJr  \(       a  SSKJr   " S S	\5      rg)
    N)TYPE_CHECKINGDictListOptionalTupleUnion   )register_to_config   )BaseGuidancerescale_noise_cfg)
BlockStatec                   D  ^  \ rS rSrSrSS/r\     SS\S\S\S\S	\4
U 4S
 jjj5       r	 SSSS\
\\\\\\\4   4   4      S\S   4S jjrSS\R$                  S\
\R$                     S\R$                  4S jjr\S\4S j5       r\S\4S j5       rS\4S jrSrU =r$ )ClassifierFreeGuidance   a
  
Classifier-free guidance (CFG): https://huggingface.co/papers/2207.12598

CFG is a technique used to improve generation quality and condition-following in diffusion models. It works by
jointly training a model on both conditional and unconditional data, and using a weighted sum of the two during
inference. This allows the model to tradeoff between generation quality and sample diversity. The original paper
proposes scaling and shifting the conditional distribution based on the difference between conditional and
unconditional predictions. [x_pred = x_cond + scale * (x_cond - x_uncond)]

Diffusers implemented the scaling and shifting on the unconditional prediction instead based on the [Imagen
paper](https://huggingface.co/papers/2205.11487), which is equivalent to what the original paper proposed in
theory. [x_pred = x_uncond + scale * (x_cond - x_uncond)]

The intution behind the original formulation can be thought of as moving the conditional distribution estimates
further away from the unconditional distribution estimates, while the diffusers-native implementation can be
thought of as moving the unconditional distribution towards the conditional distribution estimates to get rid of
the unconditional predictions (usually negative features like "bad quality, bad anotomy, watermarks", etc.)

The `use_original_formulation` argument can be set to `True` to use the original CFG formulation mentioned in the
paper. By default, we use the diffusers-native implementation that has been in the codebase for a long time.

Args:
    guidance_scale (`float`, defaults to `7.5`):
        The scale parameter for classifier-free guidance. Higher values result in stronger conditioning on the text
        prompt, while lower values allow for more freedom in generation. Higher values may lead to saturation and
        deterioration of image quality.
    guidance_rescale (`float`, defaults to `0.0`):
        The rescale factor applied to the noise predictions. This is used to improve image quality and fix
        overexposure. Based on Section 3.4 from [Common Diffusion Noise Schedules and Sample Steps are
        Flawed](https://huggingface.co/papers/2305.08891).
    use_original_formulation (`bool`, defaults to `False`):
        Whether to use the original formulation of classifier-free guidance as proposed in the paper. By default,
        we use the diffusers-native implementation that has been in the codebase for a long time. See
        [~guiders.classifier_free_guidance.ClassifierFreeGuidance] for more details.
    start (`float`, defaults to `0.0`):
        The fraction of the total number of denoising steps after which guidance starts.
    stop (`float`, defaults to `1.0`):
        The fraction of the total number of denoising steps after which guidance stops.
	pred_condpred_uncondguidance_scaleguidance_rescaleuse_original_formulationstartstopc                 H   > [         TU ]  XE5        Xl        X l        X0l        g N)super__init__r   r   r   )selfr   r   r   r   r   	__class__s         d/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/guiders/classifier_free_guidance.pyr   ClassifierFreeGuidance.__init__G   s$     	%, 0(@%    datar   input_fieldsreturnc                     Uc  U R                   nU R                  S:X  a  S/OSS/n/ n[        U R                  5       H6  nU R                  X!X5   U R                  U   5      nUR                  U5        M8     U$ )Nr   r   )_input_fieldsnum_conditionsrange_prepare_batch_input_predictionsappend)r   r"   r#   tuple_indicesdata_batchesi
data_batchs          r   prepare_inputs%ClassifierFreeGuidance.prepare_inputsV   s     --L#22a7aVt**+A,,\AQSWSjSjklSmnJ
+ , r!   c                     S nU R                  5       (       d  UnO*X-
  nU R                  (       a  UOUnX0R                  U-  -   nU R                  S:  a  [	        X1U R                  5      nU0 4$ )N        )_is_cfg_enabledr   r   r   r   )r   r   r   predshifts        r   forwardClassifierFreeGuidance.forwardc   sn    ##%%D+E $ = =9;D--55D  3&$Td6K6KLDRxr!   c                      U R                   S:H  $ Nr   )_count_prepared)r   s    r   is_conditional%ClassifierFreeGuidance.is_conditionalr   s    ##q((r!   c                 >    SnU R                  5       (       a  US-  nU$ r:   )r4   )r   r'   s     r   r'   %ClassifierFreeGuidance.num_conditionsv   s&    !!aNr!   c                    U R                   (       d  gSnU R                  bb  [        U R                  U R                  -  5      n[        U R                  U R                  -  5      nX R
                  s=:*  =(       a    U:  Os  nSnU R                  (       a"  [        R                  " U R                  S5      nO![        R                  " U R                  S5      nU=(       a    U(       + $ )NFTr3         ?)
_enabled_num_inference_stepsint_start_stop_stepr   mathiscloser   )r   is_within_rangeskip_start_stepskip_stop_stepis_closes        r   r4   &ClassifierFreeGuidance._is_cfg_enabled}   s    }}$$0!$++0I0I"IJO d.G.G!GHN-LLnLO((||D$7$7=H||D$7$7=H/x</r!   )r   r   r   )g      @r3   Fr3   rA   r   )__name__
__module____qualname____firstlineno____doc__r*   r
   floatboolr   r   r   strr   r   r   r0   torchTensorr7   propertyr<   rD   r'   r4   __static_attributes____classcell__)r   s   @r   r   r      s2   &P &}5 !$"%).AA  A #'	A
 A A A dh 08c5eTWY\T\oI]C^>^9_0`	l	 HU\\<R ^c^j^j  ) ) )   0 0 0r!   r   )rH   typingr   r   r   r   r   r   rW   configuration_utilsr
   guider_utilsr   r   "modular_pipelines.modular_pipeliner   r    r!   r   <module>ra      s/     D D  4 9 ?q0\ q0r!   