
    +h*(                         S SK JrJrJr  SSKJrJr  SSKJr   " S S\5      r	 " S S5      r
 " S	 S
\	5      r " S S\	5      r " S S\	5      r " S S\	5      r " S S\	5      rg)    )AnyDictList   )ConfigMixinregister_to_config)CONFIG_NAMEc                      ^  \ rS rSrSr\r\S	U 4S jj5       r\	S\
\   4S j5       rS\\\4   4S jrS\\\4   4S jrSrU =r$ )
PipelineCallback   a  
Base class for all the official callbacks used in a pipeline. This class provides a structure for implementing
custom callbacks and ensures that all callbacks have a consistent interface.

Please implement the following:
    `tensor_inputs`: This should return a list of tensor inputs specific to your callback. You will only be able to
    include
        variables listed in the `._callback_tensor_inputs` attribute of your pipeline class.
    `callback_fn`: This method defines the core functionality of your callback.
c                    > [         TU ]  5         Uc  Ub  Ub  Ub  [        S5      eUb0  [        U[        5      (       a  SUs=::  a  S::  d  O  [        S5      eg g )NzSEither cutoff_step_ratio or cutoff_step_index should be provided, not both or none.              ?z6cutoff_step_ratio must be a float between 0.0 and 1.0.)super__init__
ValueError
isinstancefloat)selfcutoff_step_ratiocutoff_step_index	__class__s      M/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/callbacks.pyr   PipelineCallback.__init__   sk    %*;*C).?.Krss(,e44SDU=\Y\=\UVV >] )    returnc                 2    [        SU R                   35      e)Nz2You need to set the attribute `tensor_inputs` for NotImplementedErrorr   )r   s    r   tensor_inputsPipelineCallback.tensor_inputs#   s    !$VW[WeWeVf"ghhr   c                 2    [        SU R                   35      e)Nz3You need to implement the method `callback_fn` for r   )r   pipeline
step_index	timestepscallback_kwargss        r   callback_fnPipelineCallback.callback_fn'   s    !$WX\XfXfWg"hiir   c                 &    U R                  XX45      $ N)r'   )r   r#   r$   timestepr&   s        r   __call__PipelineCallback.__call__*   s    hPPr    )r   N)__name__
__module____qualname____firstlineno____doc__r	   config_namer   r   propertyr   strr    r   r   r'   r,   __static_attributes____classcell__)r   s   @r   r   r      s}    	 KW W itCy i ijtTWY\T\~ jQ4PSUXPX> Q Qr   r   c                   b    \ rS rSrSrS\\   4S jr\S\\	   4S j5       r
S\\	\4   4S jrSrg	)
MultiPipelineCallbacks.   z
This class is designed to handle multiple pipeline callbacks. It accepts a list of PipelineCallback objects and
provides a unified interface for calling all of them.
	callbacksc                     Xl         g r*   r<   )r   r<   s     r   r   MultiPipelineCallbacks.__init__4   s    "r   r   c                 p    U R                    VVs/ s H  oR                    H  o"PM     M     snn$ s  snnf r*   )r<   r    )r   callbackinputs      r   r    $MultiPipelineCallbacks.tensor_inputs7   s+    &*nnYn(BXBXBXnYYYs   2c                 >    U R                    H  nU" XX45      nM     U$ )zb
Calls all the callbacks in order with the given arguments and returns the final callback_kwargs.
r>   )r   r#   r$   r+   r&   rA   s         r   r,   MultiPipelineCallbacks.__call__;   s&     H&xXWO ' r   r>   N)r/   r0   r1   r2   r3   r   r   r   r5   r6   r    r   r   r,   r7   r.   r   r   r:   r:   .   sR    
#$'7"8 # ZtCy Z Z4PSUXPX> r   r:   c                   6    \ rS rSrSrS/rS\\\4   4S jr	Sr
g)SDCFGCutoffCallbackE   a  
Callback function for Stable Diffusion Pipelines. After certain number of steps (set by `cutoff_step_ratio` or
`cutoff_step_index`), this callback will disable the CFG.

Note: This callback mutates the pipeline by changing the `_guidance_scale` attribute to 0.0 after the cutoff step.
prompt_embedsr   c                     U R                   R                  nU R                   R                  nUb  UO[        UR                  U-  5      nX':X  a.  X@R
                  S      nUSS  nSUl        XU R
                  S   '   U$ )Nr   r   configr   r   intnum_timestepsr    _guidance_scale)	r   r#   r$   r+   r&   r   r   cutoff_steprI   s	            r   r'   SDCFGCutoffCallback.callback_fnO   s     KK99 KK99 "3!>CH^H^arHrDs 	 $+,>,>q,ABM)"#.M'*H$5BD..q12r   r.   Nr/   r0   r1   r2   r3   r    r   r6   r   r'   r7   r.   r   r   rG   rG   E   s(     %%MdSVX[S[n r   rG   c                   8    \ rS rSrSr/ SQrS\\\4   4S jr	Sr
g)SDXLCFGCutoffCallbackb   a*  
Callback function for the base Stable Diffusion XL Pipelines. After certain number of steps (set by
`cutoff_step_ratio` or `cutoff_step_index`), this callback will disable the CFG.

Note: This callback mutates the pipeline by changing the `_guidance_scale` attribute to 0.0 after the cutoff step.
)rI   add_text_embedsadd_time_idsr   c                    U R                   R                  nU R                   R                  nUb  UO[        UR                  U-  5      nX':X  a|  X@R
                  S      nUSS  nX@R
                  S      n	U	SS  n	X@R
                  S      n
U
SS  n
SUl        XU R
                  S   '   XU R
                  S   '   XU R
                  S   '   U$ )Nr   rK   r      r   rL   )r   r#   r$   r+   r&   r   r   rQ   rI   rW   rX   s              r   r'   !SDXLCFGCutoffCallback.callback_fnp   s     KK99 KK99 "3!>CH^H^arHrDs 	 $+,>,>q,ABM)"#.M-.@.@.CDO-bc2O*+=+=a+@AL',L'*H$5BD..q125DD..q125AD..q12r   r.   NrS   r.   r   r   rU   rU   b   s%    MdSVX[S[n r   rU   c                   8    \ rS rSrSr/ SQrS\\\4   4S jr	Sr
g)SDXLControlnetCFGCutoffCallback   a0  
Callback function for the Controlnet Stable Diffusion XL Pipelines. After certain number of steps (set by
`cutoff_step_ratio` or `cutoff_step_index`), this callback will disable the CFG.

Note: This callback mutates the pipeline by changing the `_guidance_scale` attribute to 0.0 after the cutoff step.
)rI   rW   rX   imager   c                    U R                   R                  nU R                   R                  nUb  UO[        UR                  U-  5      nX':X  a  X@R
                  S      nUSS  nX@R
                  S      n	U	SS  n	X@R
                  S      n
U
SS  n
X@R
                  S      nUSS  nSUl        XU R
                  S   '   XU R
                  S   '   XU R
                  S   '   XU R
                  S   '   U$ )Nr   rK   r   rZ      r   rL   )r   r#   r$   r+   r&   r   r   rQ   rI   rW   rX   r_   s               r   r'   +SDXLControlnetCFGCutoffCallback.callback_fn   s$    KK99 KK99 "3!>CH^H^arHrDs 	 $+,>,>q,ABM)"#.M-.@.@.CDO-bc2O*+=+=a+@AL',L $$6$6q$9:E"#JE'*H$5BD..q125DD..q125AD..q125:D..q12r   r.   NrS   r.   r   r   r]   r]      s%    MdSVX[S[n r   r]   c                   4    \ rS rSrSr/ rS\\\4   4S jr	Sr
g)IPAdapterScaleCutoffCallback   aA  
Callback function for any pipeline that inherits `IPAdapterMixin`. After certain number of steps (set by
`cutoff_step_ratio` or `cutoff_step_index`), this callback will set the IP Adapter scale to `0.0`.

Note: This callback mutates the IP Adapter attention processors by setting the scale to 0.0 after the cutoff step.
r   c                     U R                   R                  nU R                   R                  nUb  UO[        UR                  U-  5      nX':X  a  UR                  S5        U$ )Nr   )rM   r   r   rN   rO   set_ip_adapter_scale)r   r#   r$   r+   r&   r   r   rQ   s           r   r'   (IPAdapterScaleCutoffCallback.callback_fn   sa     KK99 KK99 "3!>CH^H^arHrDs 	 $))#.r   r.   NrS   r.   r   r   rd   rd      s%     MdSVX[S[n r   rd   c                   8    \ rS rSrSrSS/rS\\\4   4S jr	Sr
g)	SD3CFGCutoffCallback   a   
Callback function for Stable Diffusion 3 Pipelines. After certain number of steps (set by `cutoff_step_ratio` or
`cutoff_step_index`), this callback will disable the CFG.

Note: This callback mutates the pipeline by changing the `_guidance_scale` attribute to 0.0 after the cutoff step.
rI   pooled_prompt_embedsr   c                 L   U R                   R                  nU R                   R                  nUb  UO[        UR                  U-  5      nX':X  aU  X@R
                  S      nUSS  nX@R
                  S      n	U	SS  n	SUl        XU R
                  S   '   XU R
                  S   '   U$ )Nr   rK   r   r   rL   )
r   r#   r$   r+   r&   r   r   rQ   rI   rl   s
             r   r'    SD3CFGCutoffCallback.callback_fn   s     KK99 KK99 "3!>CH^H^arHrDs 	 $+,>,>q,ABM)"#.M#23E3Ea3H#I #7$  (+H$5BD..q125ID..q12r   r.   NrS   r.   r   r   rj   rj      s+     %&<=MdSVX[S[n r   rj   N)typingr   r   r   configuration_utilsr   r   utilsr	   r   r:   rG   rU   r]   rd   rj   r.   r   r   <module>rr      sm    " " @ $Q{ $QN .* :', 'T-&6 -`#3 0 +  r   