
    biu"                         d dl Z d dlmZ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 ddlmZ ddlmZmZ erdd	lmZ  G d
 de      Zy)    N)TYPE_CHECKINGAnyDictListOptionalTupleUnion   )register_to_config)HookRegistryLayerSkipConfig)_apply_layer_skip_hook   )BaseGuidancerescale_noise_cfg)
BlockStatec                       e Zd ZdZddgZe	 	 	 	 	 	 	 	 ddedeee	e
e	   f      deee
e   eeef   f   dee   d	ed
ededef fd       Zdej$                  j&                  ddfdZdej$                  j&                  ddfdZ	 ddddeeeeeeeef   f   f      de
d   fdZddej0                  deej0                     dej0                  fdZedefd       Zede	fd       ZdefdZ xZS )AutoGuidancea  
    AutoGuidance: https://huggingface.co/papers/2406.02507

    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.
        auto_guidance_layers (`int` or `List[int]`, *optional*):
            The layer indices to apply skip layer guidance to. Can be a single integer or a list of integers. If not
            provided, `skip_layer_config` must be provided.
        auto_guidance_config (`LayerSkipConfig` or `List[LayerSkipConfig]`, *optional*):
            The configuration for the skip layer guidance. Can be a single `LayerSkipConfig` or a list of
            `LayerSkipConfig`. If not provided, `skip_layer_guidance_layers` must be provided.
        dropout (`float`, *optional*):
            The dropout probability for autoguidance on the enabled skip layers (either with `auto_guidance_layers` or
            `auto_guidance_config`). If not provided, the dropout probability will be set to 1.0.
        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_uncondNguidance_scaleauto_guidance_layersauto_guidance_configdropoutguidance_rescaleuse_original_formulationstartstopc	                 j   t         |   ||       || _        || _        || _        || _        || _        || _        ||t        d      ||t        d      ||||t        d      |Vt        |t              r|g}t        |t              st        dt        |       d      |D 	cg c]  }	t        |	d|       }}	t        |t              rt        j                  |      }t        |t              r|g}t        |t              st        dt        |       d      t        t!        t#        |      d       t              r"|D 
cg c]  }
t        j                  |
       }}
|| _        t%        t'        | j                              D cg c]  }d	| 	 c}| _        y c c}	w c c}
w c c}w )
NzgEither `auto_guidance_layers` or `auto_guidance_config` must be provided to enable Skip Layer Guidance.zMOnly one of `auto_guidance_layers` or `auto_guidance_config` can be provided.zA`dropout` must be provided if `auto_guidance_layers` is provided.zHExpected `auto_guidance_layers` to be an int or a list of ints, but got .auto)fqnr   z^Expected `auto_guidance_config` to be a LayerSkipConfig or a list of LayerSkipConfig, but got AutoGuidance_)super__init__r   r   r   r   r   r   
ValueError
isinstanceintlisttyper   dict	from_dictnextiterrangelen_auto_guidance_hook_names)selfr   r   r   r   r   r   r   r   layerconfigi	__class__s               Z/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/guiders/auto_guidance.pyr%   zAutoGuidance.__init__@   s    	%,$8!$8! 0(@%',@,Hy   +0D0PlmmO 4 @$8$@`aa+.4(<'=$2D9 ^_cdx_y^zz{|  Rf$HM67C$  $ *D1#2#<#<=Q#R *O<$8#9 .5pqu  wK  rL  qM  MN  O  T"67>ETh#i&O$=$=f$E#i #i$8!GLSQUQjQjMkGl)m!M!*=)m&%$ $j *ns   .F&F+F0denoiserreturnc                     | xj                   dz  c_         | j                         rD| j                  r7t        | j                  | j
                        D ]  \  }}t        |||        y y y )Nr   )name)_count_prepared_is_ag_enabledis_unconditionalzipr1   r   r   )r2   r8   r;   r4   s       r7   prepare_modelszAutoGuidance.prepare_models{   sc    ! T%:%: #D$B$BDD]D] ^ Df&xdCD &;     c                     | j                         rG| j                  r:| j                  D ]*  }t        j                  |      }|j                  |d       , y y y )NT)recurse)r=   r>   r1   r   check_if_exists_or_initializeremove_hook)r2   r8   r;   registrys       r7   cleanup_modelszAutoGuidance.cleanup_models   sV     T%:%:66 9'EEhO$$T4$89 &; rA   datar   input_fieldsc                     || j                   }| j                  dk(  rdgnddg}g }t        | j                        D ]7  }| j                  ||||   | j                  |         }|j                  |       9 |S )Nr   r   )_input_fieldsnum_conditionsr/   _prepare_batch_input_predictionsappend)r2   rH   rI   tuple_indicesdata_batchesr5   
data_batchs          r7   prepare_inputszAutoGuidance.prepare_inputs   s     --L#22a7aVt**+ 	,A,,\4qAQSWSjSjklSmnJ
+	, rA   c                     d }| j                         s|}n'||z
  }| j                  r|n|}|| j                  |z  z   }| j                  dkD  rt	        ||| j                        }|i fS )N        )r=   r   r   r   r   )r2   r   r   predshifts        r7   forwardzAutoGuidance.forward   sq    ""$D+E $ = =9;D$--55D  3&$T9d6K6KLDRxrA   c                      | j                   dk(  S Nr   )r<   )r2   s    r7   is_conditionalzAutoGuidance.is_conditional   s    ##q((rA   c                 4    d}| j                         r|dz  }|S rZ   )r=   )r2   rL   s     r7   rL   zAutoGuidance.num_conditions   s#     aNrA   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 )NFTrU         ?)
_enabled_num_inference_stepsr(   _start_stop_stepr   mathiscloser   )r2   is_within_rangeskip_start_stepskip_stop_stepis_closes        r7   r=   zAutoGuidance._is_ag_enabled   s    }}$$0!$++0I0I"IJO d.G.G!GHN-LnLO((||D$7$7=H||D$7$7=H/x</rA   )g      @NNNrU   FrU   r^   )N)__name__
__module____qualname____doc__rN   r   floatr   r	   r(   r   r   r   strr   boolr%   torchnnModuler@   rG   r   rS   TensorrX   propertyr[   rL   r=   __classcell__)r6   s   @r7   r   r      s   > &}5 !$@D^b#'"%).8n8n 'uS$s)^'<=8n $OT/5JDQTVYQYN$Z[	8n
 %8n  8n #'8n 8n 8n 8ntDuxx D4 D9uxx 94 9 dh 08c5eTWY\T\oI]C^>^9_0`	l	 HU\\<R ^c^j^j  ) ) )   0 0rA   r   )rd   typingr   r   r   r   r   r   r	   rq   configuration_utilsr   hooksr   r   hooks.layer_skipr   guider_utilsr   r   "modular_pipelines.modular_pipeliner   r    rA   r7   <module>r~      s8     I I I  4 1 5 9 ?`0< `0rA   