
    +h                     P    S SK r S SKJrJr  S SKrS SKJr  SSKJr   " S S5      r	g)    N)TupleUnion   )randn_tensorc                      \ rS rSrSr      SS\S\S\S\S\S\4S	 jjr	S
 r
\S 5       rS\\S4   S\\\R                   4   S\S\S\S\S\R"                  4S jrS\R"                  S\R"                  S\R"                  S\R"                  4S jrS\R"                  S\S\S\R(                  S\R                   S\R*                  4S jrSrg)FreeInitMixin   zMixin class for FreeInit.	num_itersuse_fast_samplingmethodorderspatial_stop_frequencytemporal_stop_frequencyc                 L    Xl         X l        X0l        X@l        XPl        X`l        g)a  Enables the FreeInit mechanism as in https://huggingface.co/papers/2312.07537.

This implementation has been adapted from the [official repository](https://github.com/TianxingWu/FreeInit).

Args:
    num_iters (`int`, *optional*, defaults to `3`):
        Number of FreeInit noise re-initialization iterations.
    use_fast_sampling (`bool`, *optional*, defaults to `False`):
        Whether or not to speedup sampling procedure at the cost of probably lower quality results. Enables the
        "Coarse-to-Fine Sampling" strategy, as mentioned in the paper, if set to `True`.
    method (`str`, *optional*, defaults to `butterworth`):
        Must be one of `butterworth`, `ideal` or `gaussian` to use as the filtering method for the FreeInit low
        pass filter.
    order (`int`, *optional*, defaults to `4`):
        Order of the filter used in `butterworth` method. Larger values lead to `ideal` method behaviour
        whereas lower values lead to `gaussian` method behaviour.
    spatial_stop_frequency (`float`, *optional*, defaults to `0.25`):
        Normalized stop frequency for spatial dimensions. Must be between 0 to 1. Referred to as `d_s` in the
        original implementation.
    temporal_stop_frequency (`float`, *optional*, defaults to `0.25`):
        Normalized stop frequency for temporal dimensions. Must be between 0 to 1. Referred to as `d_t` in the
        original implementation.
N)_free_init_num_iters_free_init_use_fast_sampling_free_init_method_free_init_order!_free_init_spatial_stop_frequency"_free_init_temporal_stop_frequency)selfr
   r   r   r   r   r   s          ]/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/free_init_utils.pyenable_free_initFreeInitMixin.enable_free_init   s)    @ %.!,=)!' %1G.2I/    c                     SU l         g)z+Disables the FreeInit mechanism if enabled.N)r   r   s    r   disable_free_initFreeInitMixin.disable_free_initB   s
    $(!r   c                 D    [        U S5      =(       a    U R                  S L$ )Nr   )hasattrr   r   s    r   free_init_enabledFreeInitMixin.free_init_enabledF   s!    t34^9R9RZ^9^^r   shape.devicefilter_typereturnc           	        ^^ US   US   US   pn[         R                  " U5      n
TS:X  d  US:X  a  U
$ US:X  a  UU4S jnO%US:X  a  U4S jnOUS	:X  a  U4S
 jnO[        S5      e[        U5       Hg  n[        U5       HU  n[        U	5       HC  nTU-  SU-  U-  S-
  -  S-  SU-  U-  S-
  S-  -   SU-  U	-  S-
  S-  -   nU" U5      U
SXU4'   ME     MW     Mi     U
R	                  U5      $ )zLReturns the FreeInit filter based on filter type and other input conditions.r   butterworthc                 &   > SSU TS-  -  T-  -   -  $ )N   r    )xr   r   s    r   retrieve_mask?FreeInitMixin._get_free_init_freq_filter.<locals>.retrieve_mask]   s#    A%;Q%>!>5 HHIIr   gaussianc                 H   > [         R                  " SSTS-  -  -  U -  5      $ )Nr+   r   )mathexpr0   r   s    r   r1   r2   a   s'    xxa*@!*C&C Dq HIIr   idealc                    > U TS-  ::  a  S$ S$ )Nr   r.   r   r/   r7   s    r   r1   r2   e   s    !7!!;;qBBr   z;`filter_type` must be one of gaussian, butterworth or idealr   r.   .)torchzerosNotImplementedErrorrangeto)r   r$   r%   r&   r   r   r   timeheightwidthmaskr1   thwd_squares       ``          r   _get_free_init_freq_filter(FreeInitMixin._get_free_init_freq_filterJ   s%    $BirE"Ie{{5!!Q&*AQ*FK-'JJ&JG#C &&cddtA6]uA03JJqSTuW[|^_O_`effq56>A-!34q55=1,23 
 *7x)@DaA& & #  wwvr   r0   noiselow_pass_filterc                 8   [         R                  " USS9n[         R                  " USS9n[         R                  " USS9n[         R                  " USS9nSU-
  nXC-  nXV-  nXx-   n	[         R                  " U	SS9n	[         R                  " U	SS9R
                  n
U
$ )zNoise reinitialization.)r)   r*   r+   )dimr.   )fftfftnfftshift	ifftshiftifftnreal)r   r0   rI   rJ   x_freq
noise_freqhigh_pass_filter
x_freq_lownoise_freq_highx_freq_mixedx_mixeds              r   _apply_freq_filter FreeInitMixin._apply_freq_filterv   s     !.f,7XXe6
\\*,?
 .-
$7!3 }}\|D))Ll;@@r   latentsfree_init_iterationnum_inference_stepsdtype	generatorc           	         US:X  a%  UR                  5       R                  5       U l        GO&UR                  nS/USS  Q7nU R	                  UUU R
                  U R                  U R                  U R                  S9n	U R                  R                  R                  S-
  n
[        R                  " US   4U
5      R                  5       nU R                  R                  XR                  UR!                  U5      S9R!                  [        R"                  S9n[%        UUU[        R"                  S9nU R'                  XU	S9nUR!                  U5      nU R(                  (       a'  [+        S[-        X0R.                  -  US-   -  5      5      nUS:  a  U R                  R1                  X4S9  XR                  R2                  4$ )	Nr   r.   )r$   r%   r&   r   r   r   )original_samplesrI   	timesteps)r_   )r$   r`   r%   r_   )rJ   )r%   )detachclone_free_init_initial_noiser$   rG   r   r   r   r   	schedulerconfignum_train_timestepsr:   fulllong	add_noiser>   float32r   rZ   r   maxintr   set_timestepsrc   )r   r\   r]   r^   r%   r_   r`   latent_shapefree_init_filter_shapefree_init_freq_filtercurrent_diffuse_timestepdiffuse_timestepsz_tz_rands                 r   _apply_free_initFreeInitMixin._apply_free_init   s    !#,3NN,<,B,B,DD)"==L&'%;,qr*:%;"$($C$C, 22++'+'M'M(,(O(O %D %! (,~~'<'<'P'PST'T$ %

LO+=?W X ] ] _..**!(0M0MYjYmYmntYu + bu}}b%  ""#mm	F --cK`-aGjj'G ,,"%3*-F-FFJ]`aJabc# "NN(()<(L0000r   )rf   r   r   r   r   r   r   N)   Fr,            ?r|   )__name__
__module____qualname____firstlineno____doc__ro   boolstrfloatr   r   propertyr"   r   r   r:   r_   TensorrG   rZ   r%   	Generatorrx   __static_attributes__r/   r   r   r   r      se   $ "'#(,)-%J%J  %J 	%J
 %J !&%J "'%JN) _ _*S#X* c5;;&'* 	*
 * !&* "'* 
*XELL  X]XdXd iniuiu (1111 !11 !	11
 11 {{11 ??11r   r   )
r5   typingr   r   r:   	torch.fftrM   utils.torch_utilsr   r   r/   r   r   <module>r      s"        ,c1 c1r   