
    i0                     0   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mZ erddlmZ  G d d	e      Z G d
 d      Z	 ddej&                  dej&                  dee   fdZ	 	 	 	 ddej&                  dej&                  dedee   dededefdZy)    N)TYPE_CHECKINGDictListOptionalTupleUnion   )register_to_config   )BaseGuidanceGuiderOutput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d	ed
ededededef fd       Z	de
eeej                  ej                  f   f   ded   fdZddd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fdZedefd       Zedefd       ZdefdZdefdZ fdZ xZS )AdaptiveProjectedMixGuidancea:	  
    Adaptive Projected Guidance (APG) https://huggingface.co/papers/2410.02416 combined with Classifier-Free Guidance
    (CFG). This guider is used in HunyuanImage2.1 https://github.com/Tencent-Hunyuan/HunyuanImage-2.1

    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.
        adaptive_projected_guidance_momentum (`float`, defaults to `None`):
            The momentum parameter for the adaptive projected guidance. Disabled if set to `None`.
        adaptive_projected_guidance_rescale (`float`, defaults to `15.0`):
            The rescale factor applied to the noise predictions for adaptive projected guidance. This is used to
            improve image quality and fix
        guidance_rescale (`float`, defaults to `0.0`):
            The rescale factor applied to the noise predictions for classifier-free guidance. 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 the classifier-free guidance starts.
        stop (`float`, defaults to `1.0`):
            The fraction of the total number of denoising steps after which the classifier-free guidance stops.
        adaptive_projected_guidance_start_step (`int`, defaults to `5`):
            The step at which the adaptive projected guidance starts (before this step, classifier-free guidance is
            used, and momentum buffer is updated).
        enabled (`bool`, defaults to `True`):
            Whether this guidance is enabled.
    	pred_condpred_uncondguidance_scaleguidance_rescale!adaptive_projected_guidance_scale$adaptive_projected_guidance_momentum#adaptive_projected_guidance_rescaleetause_original_formulationstartstop&adaptive_projected_guidance_start_stepenabledc                     t         |   ||	|       || _        || _        || _        || _        || _        || _        |
| _        || _	        d | _
        y N)super__init__r   r   r   r   r   r   r   r   momentum_buffer)selfr   r   r   r   r   r   r   r   r   r   r   	__class__s               {/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/guiders/adaptive_projected_guidance_mix.pyr"   z%AdaptiveProjectedMixGuidance.__init__@   s]     	g., 01R.4X13V06\3(@%#    datareturnr   c                 $   | j                   dk(  r&| j                  t        | j                        | _        | j                  dk(  rdgnddg}g }t        || j                        D ])  \  }}| j                  |||      }|j                  |       + |S Nr   r   )	_stepr   MomentumBufferr#   num_conditionszip_input_predictions_prepare_batchappend)r$   r(   tuple_indicesdata_batches	tuple_idxinput_prediction
data_batchs          r&   prepare_inputsz+AdaptiveProjectedMixGuidance.prepare_inputs[   s    ::?88D'5d6_6_'`$#22a7aV+.}d>U>U+V 	,'I',,T9>NOJ
+	, r'   input_fieldsc                 &   | j                   dk(  r&| j                  t        | j                        | _        | j                  dk(  rdgnddg}g }t        || j                        D ]*  \  }}| j                  ||||      }|j                  |       , |S r+   )	r,   r   r-   r#   r.   r/   r0   _prepare_batch_from_block_stater2   )r$   r(   r9   r3   r4   r5   r6   r7   s           r&   prepare_inputs_from_block_statez<AdaptiveProjectedMixGuidance.prepare_inputs_from_block_statef   s     ::?88D'5d6_6_'`$#22a7aV+.}d>U>U+V 	,'I'==lDR[]mnJ
+	, r'   c           	         d }| j                         s|}n| j                         sK| j                  t        ||| j                         ||z
  }| j                  r|n|}|| j
                  |z  z   }nS| j                         rCt        ||| j                  | j                  | j                  | j                  | j                        }| j                  dkD  rt        ||| j                        }t        |||      S )N        )predr   r   )_is_cfg_enabled_is_apg_enabledr#   update_momentum_bufferr   r   normalized_guidancer   r   r   r   r   r   )r$   r   r   r?   shifts        r&   forwardz$AdaptiveProjectedMixGuidance.forwards   s     ##%D %%'##/&y+t?S?ST+E $ = =9;D$--55D !!#&66$$88--D   3&$T9d6K6KLDTTr'   c                      | j                   dk(  S Nr   )_count_prepared)r$   s    r&   is_conditionalz+AdaptiveProjectedMixGuidance.is_conditional   s    ##q((r'   c                 T    d}| j                         s| j                         r|dz  }|S rG   )rA   r@   )r$   r.   s     r&   r.   z+AdaptiveProjectedMixGuidance.num_conditions   s.    !T%9%9%;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 )NFTr>         ?)
_enabled_num_inference_stepsint_start_stopr,   r   mathiscloser   )r$   is_within_rangeskip_start_stepskip_stop_stepis_closes        r&   r@   z,AdaptiveProjectedMixGuidance._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'   c                 8   | j                   sy| j                         syd}| j                  | j                  | j                  kD  }d}| j                  r!t        j                  | j                  d      }n t        j                  | j                  d      }|xr | S )NFr>   rL   )rM   r@   r,   r   r   rR   rS   r   )r$   rT   rW   s      r&   rA   z,AdaptiveProjectedMixGuidance._is_apg_enabled   s    }}##%::!"jj4+V+VVO((||D$J$JCPH||D$J$JCPH/x</r'   c                     t         |          }| j                  |d<   | j                         |d<   | j	                         |d<   |S )Nr#   is_apg_enabledis_cfg_enabled)r!   	get_stater#   rA   r@   )r$   stater%   s     r&   r\   z&AdaptiveProjectedMixGuidance.get_state   sN    !##'#7#7 "&"6"6"8"&"6"6"8r'   )g      @r>         $@g      r^   r>   Fr>   rL      Tr    )__name__
__module____qualname____doc__r0   r
   floatboolrO   r"   r   strr   torchTensorr   r8   r   r<   r   r   rE   propertyrI   r.   r@   rA   r\   __classcell__)r%   s   @r&   r   r      s   B &}5 !$"%376:59).67$$  $ ,1	$
 /4$ .3$ $ #'$ $ $ 14$ $ $4	4U5<<3M-N(N#O 	TXYeTf 	 04S%U3PS8_@T:U5U0V	l	U UHU\\<R U^j UB ) ) )   0 0$0 0& r'   r   c                   D    e Zd ZdefdZdej                  fdZdefdZ	y)r-   momentumc                      || _         d| _        y )Nr   rl   running_average)r$   rl   s     r&   r"   zMomentumBuffer.__init__   s      r'   update_valuec                 J    | j                   | j                  z  }||z   | _        y r    rn   )r$   rp   new_averages      r&   updatezMomentumBuffer.update   s#    mmd&:&::+k9r'   r)   c           
         t        | j                  t        j                        rt	        | j                  j
                        }t        j                         5  | j                  j                         j                         | j                  j                         j                         | j                  j                         j                         | j                  j                         j                         d}ddd       t	        d |D              }| j                  |   }t        |j                         j                         j                         j!                               }t#        |      dkD  r|dd dz   }dj%                  j'                         D cg c]  \  }}| d|d c}}      }d	| j(                   d
| d| d| d	S d| j(                   d| j                   dS # 1 sw Y   xY wc c}}w )zz
        Returns a string representation showing momentum, shape, statistics, and a slice of the running_average.
        )meanstdminmaxNc              3   H   K   | ]  }t        d t        d|              y w)N   )slicerw   ).0dims     r&   	<genexpr>z*MomentumBuffer.__repr__.<locals>.<genexpr>   s     !Ls%c!Sk":!Ls    "   z...z, =z.4fzMomentumBuffer(
  momentum=z
,
  shape=z,
  stats=[z],
  slice=z
)zMomentumBuffer(momentum=z, running_average=))
isinstancero   rg   rh   tupleshapeno_gradru   itemrv   rw   rx   rf   detachrd   cpunumpylenjoinitemsrl   )	r$   r   statsslice_indicessliced_data	slice_strkv	stats_strs	            r&   __repr__zMomentumBuffer.__repr__   s    d**ELL9$..445E   00557<<>//335::<//335::<//335::<	 "!Le!LLM..}=K K..0668<<>DDFGI9~#%dsOe3			ekkm"LdaaS!C>"LMI"mm_ - ' "%; '$+ &	 .dmm_<NtOcOcNddeff9 " #Ms   B G)G5
)G2N)
r`   ra   rb   rd   r"   rg   rh   rs   rf   r    r'   r&   r-   r-      s,    ! !:5<< :$g# $gr'   r-   r   r   r#   c                 6    | |z
  }||j                  |       y y r    )rs   )r   r   r#   diffs       r&   rB   rB      s'    
 {"D"t$ #r'   r   r   norm_thresholdr   c                    |t        | ||       |j                  }n| |z
  }t        dt        |j                              D cg c]  }|  }	}|dkD  rGt        j                  |      }
|j                  d|	d      }t        j                  |
||z        }||z  }|j                         | j                         }}t
        j                  j                  j                  ||	      }||z  j                  |	d      |z  }||z
  }|j                  |      |j                  |      }}|||z  z   }|r| n|}|||z  z   }|S c c}w )Nr   r   r	   T)pr}   keepdim)r}   )r}   r   )rB   ro   ranger   r   rg   	ones_likenormminimumdoublenn
functional	normalizesumtype_as)r   r   r   r#   r   r   r   r   ir}   ones	diff_normscale_factorv0v1v0_parallelv0_orthogonaldiff_paralleldiff_orthogonalnormalized_updater?   s                        r&   rC   rC     sG    "y+G..;&QDJJ0
1!A2
1C
1t$IIsDI9	}}T>I+EFl"[[]I,,.B				&	&rs	&	3B7--C-6;K$M%0%8%8%>@U@UVZ@[?M'#*==09kD.#444DK% 2s   
D>r    )NrL   r>   F)rR   typingr   r   r   r   r   r   rg   configuration_utilsr
   guider_utilsr   r   r   "modular_pipelines.modular_pipeliner   r   r-   rh   rB   rd   re   rC   r   r'   r&   <module>r      s     D D  4 G G ?n< nd-g -gf 15%||%% n-% 15%*!||!! ! n-	!
 
! ! #!r'   