
    bi*(                         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 G d de	      Z G d de	      Zy)    )AnyDictList   )ConfigMixinregister_to_config)CONFIG_NAMEc                   z     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.
    c                     t         |           ||||t        d      |3t        |t              rd|cxk  rdk  st        d       t        d      y y )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      N/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/callbacks.pyr   zPipelineCallback.__init__   sz    %*;*C).?.Krss(,e4SDU=\Y\=\UVV >]UVV >] )    returnc                 2    t        d| j                         )Nz2You need to set the attribute `tensor_inputs` for NotImplementedErrorr   )r   s    r   tensor_inputszPipelineCallback.tensor_inputs#   s    !$VW[WeWeVf"ghhr   c                 2    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    !$WX\XfXfWg"hiir   c                 *    | j                  ||||      S N)r$   )r   r    r!   timestepr#   s        r   __call__zPipelineCallback.__call__*   s    *hPPr   )r   N)__name__
__module____qualname____doc__r	   config_namer   r   propertyr   strr   r   r   r$   r(   __classcell__)r   s   @r   r   r      sw    	 KW W itCy i ijtTWY\T\~ jQ4PSUXPX> Qr   r   c                   T    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y)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                     || _         y r&   r3   )r   r3   s     r   r   zMultiPipelineCallbacks.__init__4   s	    "r   r   c                 h    | j                   D cg c]  }|j                  D ]  }|  c}}S c c}}w r&   )r3   r   )r   callbackinputs      r   r   z$MultiPipelineCallbacks.tensor_inputs7   s,    &*nnY((BXBXYYYYYs   .c                 >    | j                   D ]  } |||||      } |S )zr
        Calls all the callbacks in order with the given arguments and returns the final callback_kwargs.
        r5   )r   r    r!   r'   r#   r7   s         r   r(   zMultiPipelineCallbacks.__call__;   s1      	XH&xXWO	X r   N)r)   r*   r+   r,   r   r   r   r.   r/   r   r   r   r(    r   r   r2   r2   .   sR    
#$'7"8 # ZtCy Z Z4PSUXPX> r   r2   c                   ,    e Zd ZdZdgZdeeef   fdZy)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(  r0|| 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_stepr=   s	            r   r$   zSDCFGCutoffCallback.callback_fnO   s     KK99 KK99 "3!>CH^H^arHrDs 	 $+D,>,>q,ABM)"#.M'*H$5BOD..q12r   N	r)   r*   r+   r,   r   r   r/   r   r$   r:   r   r   r<   r<   E   s(     %%MdSVX[S[n r   r<   c                   .    e Zd ZdZg dZdeeef   fdZy)SDXLCFGCutoffCallbacka:  
    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.
    )r=   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   r?   r      r   r@   )r   r    r!   r'   r#   r   r   rE   r=   rI   rJ   s              r   r$   z!SDXLCFGCutoffCallback.callback_fnp   s     KK99 KK99 "3!>CH^H^arHrDs 	 $+D,>,>q,ABM)"#.M-d.@.@.CDO-bc2O*4+=+=a+@AL',L'*H$5BOD..q125DOD..q125AOD..q12r   NrF   r:   r   r   rH   rH   b   s%    MdSVX[S[n r   rH   c                   .    e Zd ZdZg dZdeeef   fdZy)SDXLControlnetCFGCutoffCallbacka@  
    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.
    )r=   rI   rJ   imager   c                    | j                   j                  }| j                   j                  }||nt        |j                  |z        }||k(  r|| j
                  d      }|dd  }|| j
                  d      }	|	dd  }	|| j
                  d      }
|
dd  }
|| j
                  d      }|dd  }d|_        ||| j
                  d   <   |	|| j
                  d   <   |
|| j
                  d   <   ||| j
                  d   <   |S )Nr   r?   r   rL      r   r@   )r   r    r!   r'   r#   r   r   rE   r=   rI   rJ   rO   s               r   r$   z+SDXLControlnetCFGCutoffCallback.callback_fn   s6    KK99 KK99 "3!>CH^H^arHrDs 	 $+D,>,>q,ABM)"#.M-d.@.@.CDO-bc2O*4+=+=a+@AL',L $D$6$6q$9:E"#JE'*H$5BOD..q125DOD..q125AOD..q125:OD..q12r   NrF   r:   r   r   rN   rN      s%    MdSVX[S[n r   rN   c                   *    e Zd ZdZg Zdeeef   fdZy)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|j                  d       |S )Nr   )rA   r   r   rB   rC   set_ip_adapter_scale)r   r    r!   r'   r#   r   r   rE   s           r   r$   z(IPAdapterScaleCutoffCallback.callback_fn   sc     KK99 KK99 "3!>CH^H^arHrDs 	 $))#.r   NrF   r:   r   r   rS   rS      s%     MdSVX[S[n r   rS   c                   .    e Zd ZdZddgZdeeef   fdZy)SD3CFGCutoffCallbacka0  
    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.
    r=   pooled_prompt_embedsr   c                 R   | j                   j                  }| j                   j                  }||nt        |j                  |z        }||k(  rY|| j
                  d      }|dd  }|| j
                  d      }	|	dd  }	d|_        ||| j
                  d   <   |	|| j
                  d   <   |S )Nr   r?   r   r   r@   )
r   r    r!   r'   r#   r   r   rE   r=   rX   s
             r   r$   z SD3CFGCutoffCallback.callback_fn   s     KK99 KK99 "3!>CH^H^arHrDs 	 $+D,>,>q,ABM)"#.M#243E3Ea3H#I #7$  (+H$5BOD..q125IOD..q12r   NrF   r:   r   r   rW   rW      s+     %&<=MdSVX[S[n r   rW   N)typingr   r   r   configuration_utilsr   r   utilsr	   r   r2   r<   rH   rN   rS   rW   r:   r   r   <module>r]      sm    " " @ $Q{ $QN .* :', 'T-&6 -`#3 0 +  r   