
    bi                     v    d dl Z d dlmZmZmZmZmZmZ d dlZddl	m
Z
 ddlmZmZ erddlmZ  G d d	e      Zy)
    N)TYPE_CHECKINGDictListOptionalTupleUnion   )register_to_config   )BaseGuidancerescale_noise_cfg)
BlockStatec                   (    e Zd ZdZddgZe	 	 	 	 	 ddededededef
 fd	       Z	 dd
dde	e
eeeeeef   f   f      ded   fdZddej"                  de	ej"                     dej"                  fdZedefd       Zedefd       ZdefdZ xZS )ClassifierFreeGuidanceas  
    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                 P    t         |   ||       || _        || _        || _        y N)super__init__r   r   r   )selfr   r   r   r   r   	__class__s         e/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/guiders/classifier_free_guidance.pyr   zClassifierFreeGuidance.__init__G   s,     	%, 0(@%    datar   input_fieldsreturnc                     || j                   }| j                  dk(  rdgnddg}g }t        | j                        D ]7  }| j                  ||||   | j                  |         }|j                  |       9 |S )Nr   r   )_input_fieldsnum_conditionsrange_prepare_batch_input_predictionsappend)r   r    r!   tuple_indicesdata_batchesi
data_batchs          r   prepare_inputsz%ClassifierFreeGuidance.prepare_inputsV   s     --L#22a7aVt**+ 	,A,,\4qAQSWSjSjklSmnJ
+	, r   c                     d }| j                         s|}n'||z
  }| j                  r|n|}|| j                  |z  z   }| j                  dkD  rt	        ||| j                        }|i fS )N        )_is_cfg_enabledr   r   r   r   )r   r   r   predshifts        r   forwardzClassifierFreeGuidance.forwardc   sq    ##%D+E $ = =9;D$--55D  3&$T9d6K6KLDRxr   c                      | j                   dk(  S Nr   )_count_prepared)r   s    r   is_conditionalz%ClassifierFreeGuidance.is_conditionalr   s    ##q((r   c                 4    d}| j                         r|dz  }|S r6   )r1   )r   r%   s     r   r%   z%ClassifierFreeGuidance.num_conditionsv   s#    !aNr   c                    | j                   syd}| j                  ^t        | j                  | j                  z        }t        | j                  | j                  z        }|| j
                  cxk  xr |k  nc }d}| j                  r!t        j                  | j                  d      }n t        j                  | j                  d      }|xr | S )NFTr0         ?)
_enabled_num_inference_stepsint_start_stop_stepr   mathiscloser   )r   is_within_rangeskip_start_stepskip_stop_stepis_closes        r   r1   z&ClassifierFreeGuidance._is_cfg_enabled}   s    }}$$0!$++0I0I"IJO d.G.G!GHN-LnLO((||D$7$7=H||D$7$7=H/x</r   )g      @r0   Fr0   r;   r   )__name__
__module____qualname____doc__r(   r
   floatboolr   r   r   strr   r   r   r.   torchTensorr4   propertyr8   r>   r%   r1   __classcell__)r   s   @r   r   r      s-   &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 0r   r   )rB   typingr   r   r   r   r   r   rO   configuration_utilsr
   guider_utilsr   r   "modular_pipelines.modular_pipeliner   r    r   r   <module>rX      s/     D D  4 9 ?q0\ q0r   