
    wi                     X    d dl Z d dlmZmZ d dlZd dlmZ ddlmZ  G d d          Z	dS )    N)TupleUnion   )randn_tensorc                   <   e Zd ZdZ	 	 	 	 	 	 d!deded	ed
ededefdZd Z	e
d             Zdeedf         deeej        f         ded
edededej        fdZdej        dej        dej        dej        fdZdej        dededej        dej        dej        fdZd S )"FreeInitMixinzMixin class for FreeInit.   Fbutterworth         ?	num_itersuse_fast_samplingmethodorderspatial_stop_frequencytemporal_stop_frequencyc                 Z    || _         || _        || _        || _        || _        || _        dS )a   Enables the FreeInit mechanism as in https://arxiv.org/abs/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          s/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/pipelines/free_init_utils.pyenable_free_initzFreeInitMixin.enable_free_init   s;    @ %.!,=)!' %1G.2I///    c                     d| _         dS )z+Disables the FreeInit mechanism if enabled.N)r   r   s    r   disable_free_initzFreeInitMixin.disable_free_initB   s    $(!!!r   c                 4    t          | d          o| j        d uS )Nr   )hasattrr   r   s    r   free_init_enabledzFreeInitMixin.free_init_enabledF   s!    t344^9RZ^9^^r   shape.devicefilter_typereturnc           	          |d         |d         |d         }	}}t          j        |          }
dk    s|dk    r|
S |dk    rfd}n'|dk    rfd}n|d	k    rfd
}nt          d          t          |          D ]j}t          |          D ]X}t          |	          D ]F}|z  d|z  |z  dz
  z  dz  d|z  |z  dz
  dz  z   d|z  |	z  dz
  dz  z   } ||          |
d|||f<   GYk|
                    |          S )zLReturns the FreeInit filter based on filter type and other input conditions.r   r
   c                 &    dd| dz  z  z  z   z  S )N   r    )xr   r   s    r   retrieve_maskz?FreeInitMixin._get_free_init_freq_filter.<locals>.retrieve_mask]   s#    A%;Q%>!>5 HHIIr   gaussianc                 D    t          j        dddz  z  z  | z            S )Nr+   r   )mathexpr/   r   s    r   r0   z?FreeInitMixin._get_free_init_freq_filter.<locals>.retrieve_maska   s(    xa*@!*C&C Dq HIIIr   idealc                     | dz  k    rdndS )Nr   r-   r   r.   r5   s    r   r0   z?FreeInitMixin._get_free_init_freq_filter.<locals>.retrieve_maske   s    !7!!;;;qq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maskr0   thwd_squares       ``          r   _get_free_init_freq_filterz(FreeInitMixin._get_free_init_freq_filterJ   s    $BirE"Ief{5!!!Q&&*AQ*F*FK-''J J J J J J JJ&&J J J J J JG##C C C C C C &&cdddt 	A 	AA6]] A Au A AA03JJqSTuW[|^_O_`effq56>A-!34q55=1,23 
 *7x)@)@DaA&&AA wwvr   r/   noiselow_pass_filterc                 @   t          j        |d          }t          j        |d          }t          j        |d          }t          j        |d          }d|z
  }||z  }||z  }||z   }	t          j        |	d          }	t          j        |	d          j        }
|
S )zNoise reinitialization.)r)   r*   r+   )dimr-   )fftfftnfftshift	ifftshiftifftnreal)r   r/   rF   rG   x_freq
noise_freqhigh_pass_filter
x_freq_lownoise_freq_highx_freq_mixedx_mixeds              r   _apply_freq_filterz FreeInitMixin._apply_freq_filterv   s     !...f,777Xe666
\*,???
 .o-
$'77!O3 }\|DDD)Ll;;;@r   latentsfree_init_iterationnum_inference_stepsdtype	generatorc                 f   |dk    r-|                                                                 | _        n#|j        }dg|dd          R }|                     ||| j        | j        | j        | j                  }	| j	        j
        j        dz
  }
t          j        |d         f|
                                          }| j	                            || j        |                    |                                        t          j                  }t%          |||t          j                  }|                     |||	          }|                    |          }| j        rGt+          dt-          || j        z  |dz   z                      }| j	                            ||           || j	        j        fS )	Nr   r-   )r$   r%   r&   r   r   r   )original_samplesrF   	timesteps)r[   )r$   r\   r%   r[   )rG   )r%   )detachclone_free_init_initial_noiser$   rE   r   r   r   r   	schedulerconfignum_train_timestepsr8   fulllong	add_noiser<   float32r   rW   r   maxintr   set_timestepsr_   )r   rX   rY   rZ   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_initzFreeInitMixin._apply_free_init   s    !##,3NN,<,<,B,B,D,DD))"=L&'%;,qrr*:%;%;"$($C$C, 2+'+'M(,(O %D % %! (,~'<'PST'T$ %
LO+=?W X X ] ] _ _.**!(0MYjYmYmntYuYu +  bu}b%%  ""#m	  F --c6K`-aaGjj''G , 	M"%3*T-FFJ]`aJabcc# # N(()<V(LLL000r   N)r	   Fr
   r   r   r   )__name__
__module____qualname____doc__rk   boolstrfloatr   r    propertyr#   r   r   r8   r[   TensorrE   rW   r%   	Generatorrt   r.   r   r   r   r      s       $$ "'#(,)-%J %J%J  %J 	%J
 %J !&%J "'%J %J %J %JN) ) ) _ _ X_*S#X* c5;&'* 	*
 * !&* "'* 
* * * *XEL  X]Xd iniu    (/1/1 !/1 !	/1
 /1 {/1 ?/1 /1 /1 /1 /1 /1r   r   )
r3   typingr   r   r8   	torch.fftrJ   utils.torch_utilsr   r   r.   r   r   <module>r      s                    , , , , , ,a1 a1 a1 a1 a1 a1 a1 a1 a1 a1r   