
    +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 SS
\R"                  S\R"                  S\S\S\R"                  4
S jj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$ ) TangentialClassifierFreeGuidance   aE  
Tangential Classifier Free Guidance (TCFG): https://huggingface.co/papers/2503.18137

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         o/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/guiders/tangential_classifier_free_guidance.pyr   )TangentialClassifierFreeGuidance.__init__5   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/TangentialClassifierFreeGuidance.prepare_inputsD   s     --L#22a7aVt**+A,,\AQSWSjSjklSmnJ
+ , r!   c                     S nU R                  5       (       d  UnO![        XU R                  U R                  5      nU R                  S:  a  [        X1U R                  5      nU0 4$ )N        )_is_tcfg_enablednormalized_guidancer   r   r   r   )r   r   r   preds       r   forward(TangentialClassifierFreeGuidance.forwardQ   s_    $$&&D&yt?R?RTXTqTqrD  3&$Td6K6KLDRxr!   c                      U R                   S:H  $ Nr   )_num_outputs_prepared)r   s    r   is_conditional/TangentialClassifierFreeGuidance.is_conditional^   s    ))Q..r!   c                 >    SnU R                  5       (       a  US-  nU$ r:   )r4   )r   r'   s     r   r'   /TangentialClassifierFreeGuidance.num_conditionsb   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   1TangentialClassifierFreeGuidance._is_tcfg_enabledi   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      s1   , &}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   r   r   r   r   r$   c                 T   U R                   n[        R                  " X/SS9R                  5       nUR	                  S5      n[        R
                  R                  USS9u  pgnUR                  5       n	SU	S S 2S4'   UR                  UR                  S5      SS5      R                  5       n
[        R                  " XR                  SS5      5      n[        R                  " X5      nUR                  UR                  5      R                  U5      nU(       a  U OUnX-
  nXU-  -   nU$ )	Nr   )dimr	   F)full_matricesr   )dtyperW   stackrT   flattenlinalgsvdclonereshapesizematmul	transposeshapeto)r   r   r   r   
cond_dtypepredsUSVhVh_modifieduncond_flatx_Vhx_Vh_Vr6   shifts                  r   r5   r5   |   s     JKK0a8>>@EMM!E||U;HA"((*KK1%%k&6&6q&91bAGGIK<<\\"b%9:D\\$,F..!2!2366zBK09kD#E5((DKr!   )F)rH   typingr   r   r   r   r   r   rW   configuration_utilsr
   guider_utilsr   r   "modular_pipelines.modular_pipeliner   r   rX   rT   rU   r5    r!   r   <module>r|      sp     D D  4 9 ?]0| ]0B qv||*/,,HMim
\\r!   