
    wi=                         d dl mZmZmZ ddlmZmZ ddlmZ  G d de          Z	 G d d          Z
 G d	 d
e	          Z G d de	          Z G d de	          ZdS )    )AnyDictList   )ConfigMixinregister_to_config)CONFIG_NAMEc                        e Zd ZdZeZed	 fd	            Zede	e
         fd            Zdee
ef         fdZdee
ef         fdZ xZS )
PipelineCallbacka+  
    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.
          ?Nc                     t                                                       ||||t          d          |3t          |t                    rd|cxk    rdk    sn t          d          d S d S )NzSEither cutoff_step_ratio or cutoff_step_index should be provided, not both or none.        r   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      c/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/callbacks.pyr   zPipelineCallback.__init__   s    %*;*C).?.Krsss(,e44 )=@DU=\=\=\=\Y\=\=\=\=\UVVV )(=\=\    returnc                 0    t          d| j                   )Nz2You need to set the attribute `tensor_inputs` for NotImplementedErrorr   r   s    r   tensor_inputszPipelineCallback.tensor_inputs#   s    !"gW[We"g"ghhhr   c                 0    t          d| j                   )Nz3You need to implement the method `callback_fn` for r   )r   pipeline
step_index	timestepscallback_kwargss        r   callback_fnzPipelineCallback.callback_fn'   s    !"hX\Xf"h"hiiir   c                 2    |                      ||||          S N)r%   )r   r!   r"   timestepr$   s        r   __call__zPipelineCallback.__call__*   s    *hPPPr   )r   N)__name__
__module____qualname____doc__r	   config_namer   r   propertyr   strr   r   r   r%   r)   __classcell__)r   s   @r   r   r      s        	 	 KW W W W W W itCy i i i XijtTWY\T\~ j j j jQ4PSUXPX> Q Q Q Q Q Q Q Qr   r   c                   n    e Zd ZdZdee         fdZedee         fd            Z	de
eef         fdZdS )MultiPipelineCallbacksz
    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                     || _         d S r'   r4   )r   r4   s     r   r   zMultiPipelineCallbacks.__init__4   s    "r   r   c                 $    d | j         D             S )Nc                 &    g | ]}|j         D ]}|S  )r   ).0callbackinputs      r   
<listcomp>z8MultiPipelineCallbacks.tensor_inputs.<locals>.<listcomp>9   s(    YYY((BXYYYYYYr   r6   r   s    r   r   z$MultiPipelineCallbacks.tensor_inputs7   s    YYdnYYYYr   c                 6    | j         D ]} |||||          }|S )zr
        Calls all the callbacks in order with the given arguments and returns the final callback_kwargs.
        r6   )r   r!   r"   r(   r$   r;   s         r   r)   zMultiPipelineCallbacks.__call__;   s4      	X 	XH&hxXWWOOr   N)r*   r+   r,   r-   r   r   r   r/   r0   r   r   r   r)   r9   r   r   r3   r3   .   s         
#$'7"8 # # # # ZtCy Z Z Z XZ4PSUXPX>      r   r3   c                   4    e Zd ZdZdgZdeeef         fdZdS )SDCFGCutoffCallbacka.  
    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                     | j         j        }| j         j        }||nt          |j        |z            }||k    r4|| j        d                  }|dd          }d|_        ||| j        d         <   |S )Nr   r   configr   r   intnum_timestepsr   _guidance_scale)	r   r!   r"   r(   r$   r   r   cutoff_steprA   s	            r   r%   zSDCFGCutoffCallback.callback_fnO   s     K9 K9 "3!>CH^arHrDsDs 	 $$+D,>q,ABM)"##.M'*H$5BOD.q12r   N	r*   r+   r,   r-   r   r   r0   r   r%   r9   r   r   r@   r@   E   sN          %%MdSVX[S[n      r   r@   c                   6    e Zd ZdZg dZdeeef         fdZdS )SDXLCFGCutoffCallbacka1  
    Callback function for 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.
    )rA   add_text_embedsadd_time_idsr   c                    | j         j        }| j         j        }||nt          |j        |z            }||k    r|| j        d                  }|dd          }|| j        d                  }	|	dd          }	|| j        d                  }
|
dd          }
d|_        ||| j        d         <   |	|| j        d         <   |
|| j        d         <   |S )Nr   rC   r      r   rD   )r   r!   r"   r(   r$   r   r   rI   rA   rM   rN   s              r   r%   z!SDXLCFGCutoffCallback.callback_fnl   s     K9 K9 "3!>CH^arHrDsDs 	 $$+D,>q,ABM)"##.M-d.@.CDO-bcc2O*4+=a+@AL',L'*H$5BOD.q125DOD.q125AOD.q12r   NrJ   r9   r   r   rL   rL   b   sR          IHHMdSVX[S[n      r   rL   c                   2    e Zd ZdZg Zdeeef         fdZdS )IPAdapterScaleCutoffCallbackaQ  
    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                     | j         j        }| j         j        }||nt          |j        |z            }||k    r|                    d           |S )Nr   )rE   r   r   rF   rG   set_ip_adapter_scale)r   r!   r"   r(   r$   r   r   rI   s           r   r%   z(IPAdapterScaleCutoffCallback.callback_fn   se     K9 K9 "3!>CH^arHrDsDs 	 $$))#...r   NrJ   r9   r   r   rR   rR      sK          MdSVX[S[n      r   rR   N)typingr   r   r   configuration_utilsr   r   utilsr	   r   r3   r@   rL   rR   r9   r   r   <module>rX      s4   " " " " " " " " " " @ @ @ @ @ @ @ @      $Q $Q $Q $Q $Q{ $Q $Q $QN       .    *   :" " " " ", " " "J    #3     r   