
    
3j                         S SK r S SKJr  S SKrSSKJr  SSKJrJrJ	r	  \(       a  SSK
Jr   " S S	\5      r  SS
\R                  S\R                  S\S\S\4
S jjrg)    N)TYPE_CHECKING   )register_to_config   )BaseGuidanceGuiderOutputrescale_noise_cfg)
BlockStatec                     ^  \ rS rSrSrSS/r\       SS\S\S\S\S	\S
\S\4U 4S jjj5       r	S\
\\\R                  \R                  4   4   S\S   4S jrSSS\
\\\\\4   -  4   S\S   4S jrSS\R                  S\R                  S-  S\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$ )MagnitudeAwareGuidance   a8  
Magnitude-Aware Mitigation for Boosted Guidance (MAMBO-G): https://huggingface.co/papers/2508.03442

Args:
    guidance_scale (`float`, defaults to `10.0`):
        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.
    alpha (`float`, defaults to `8.0`):
        The alpha parameter for the magnitude-aware guidance. Higher values cause more aggressive supression of
        guidance scale when the magnitude of the guidance update is large.
    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alphaguidance_rescaleuse_original_formulationstartstopenabledc                 V   > [         TU ]  XVU5        Xl        X l        X0l        X@l        g N)super__init__r   r   r   r   )	selfr   r   r   r   r   r   r   	__class__s	           d/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/guiders/magnitude_aware_guidance.pyr   MagnitudeAwareGuidance.__init__8   s+     	g.,
 0(@%    datareturnr
   c                     U R                   S:X  a  S/OSS/n/ n[        X R                  5       H(  u  pEU R                  XU5      nUR	                  U5        M*     U$ Nr   r   )num_conditionszip_input_predictions_prepare_batchappend)r   r    tuple_indicesdata_batches	tuple_idxinput_prediction
data_batchs          r   prepare_inputs%MagnitudeAwareGuidance.prepare_inputsJ   sc    #22a7aV+.}>U>U+V'I,,T>NOJ
+ ,W r   input_fieldsc                     U R                   S:X  a  S/OSS/n/ n[        X0R                  5       H(  u  pVU R                  X!XV5      nUR	                  U5        M*     U$ r#   )r$   r%   r&   _prepare_batch_from_block_stater(   )r   r    r0   r)   r*   r+   r,   r-   s           r   prepare_inputs_from_block_state6MagnitudeAwareGuidance.prepare_inputs_from_block_stateR   se      $22a7aV+.}>U>U+V'I==lR[nJ
+ ,W r   Nc                     S nU R                  5       (       d  UnO-[        UUU R                  U R                  U R                  5      nU R
                  S:  a  [        X1U R
                  5      n[        X1US9$ )N        )predr   r   )_is_mambo_g_enabledmambo_guidancer   r   r   r   r	   r   )r   r   r   r7   s       r   forwardMagnitudeAwareGuidance.forward\   ss    ''))D!##

--D   3&$Td6K6KLDTTr   c                      U R                   S:H  $ Nr   )_count_prepared)r   s    r   is_conditional%MagnitudeAwareGuidance.is_conditionalo   s    ##q((r   c                 >    SnU R                  5       (       a  US-  nU$ r=   )r8   )r   r$   s     r   r$   %MagnitudeAwareGuidance.num_conditionss   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(       + $ )NFTr6         ?)
_enabled_num_inference_stepsint_start_stop_stepr   mathiscloser   )r   is_within_rangeskip_start_stepskip_stop_stepis_closes        r   r8   *MagnitudeAwareGuidance._is_mambo_g_enabledz   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   r   )g      $@       @r6   Fr6   rD   Tr   )__name__
__module____qualname____firstlineno____doc__r&   r   floatboolr   dictstrtupletorchTensorlistr.   r3   r   r:   propertyr?   rG   r$   r8   __static_attributes____classcell__)r   s   @r   r   r      sm   2 &}5 !%"%).AA A  	A
 #'A A A A A"4U5<<3M-N(N#O TXYeTf  04S#c3h:O5O0P	l	U UELL4<O U[g U& ) ) )   0T 0 0r   r   r   r   r   r   r   c                 f   [        [        S[        U R                  5      5      5      nX-
  n[        R
                  " XeSS9[        R
                  " XSS9-  nU(       a  U[        R                  " U* U-  5      -  O"SUS-
  [        R                  " U* U-  5      -  -   nU(       a  U OUn	XU-  -   n	U	$ )Nr   T)dimkeepdimrD   )r_   rangelenshaper]   normexp)
r   r   r   r   r   rd   diffratioguidance_scale_finalr7   s
             r   r9   r9      s     uQIOO,-
.C"DJJtd3ejj_c6ddE $ 	E6E>22NS(EIIufun,EEE 
 19kD--DKr   )rR   F)rK   typingr   r]   configuration_utilsr   guider_utilsr   r   r	   "modular_pipelines.modular_pipeliner
   r   r^   rX   rY   r9    r   r   <module>rs      ss        4 G G ?n0\ n0j %*||  	
 #r   