o
    pi=                     @   s   d dl mZmZmZ ddlmZmZ ddlmZ G dd deZ	G dd dZ
G d	d
 d
e	ZG dd de	ZG dd de	ZdS )    )AnyDictList   )ConfigMixinregister_to_config)CONFIG_NAMEc                       sl   e Zd ZdZeZed fdd	Zede	e
 fddZdee
ef fd	d
Zdee
ef fddZ  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                    sp   t    |d u r|d u s|d ur|d urtd|d ur4t|tr0d|  kr/dks6td 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__ Q/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/callbacks.pyr      s   
zPipelineCallback.__init__returnc                 C      t d| j )Nz2You need to set the attribute `tensor_inputs` for NotImplementedErrorr   r   r   r   r   tensor_inputs#      zPipelineCallback.tensor_inputsc                 C   r   )Nz3You need to implement the method `callback_fn` for r   )r   pipeline
step_index	timestepscallback_kwargsr   r   r   callback_fn'      zPipelineCallback.callback_fnc                 C   s   |  ||||S N)r#   )r   r   r    timestepr"   r   r   r   __call__*   r$   zPipelineCallback.__call__)r
   N)__name__
__module____qualname____doc__r   config_namer   r   propertyr   strr   r   r   r#   r'   __classcell__r   r   r   r   r	      s    r	   c                   @   sN   e Zd ZdZdee fddZedee fddZ	de
eef fd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                 C   s
   || _ d S r%   r1   )r   r1   r   r   r   r   4   s   
zMultiPipelineCallbacks.__init__r   c                 C   s   dd | j D S )Nc                 S   s   g | ]
}|j D ]}|qqS r   )r   ).0callbackinputr   r   r   
<listcomp>9   s    z8MultiPipelineCallbacks.tensor_inputs.<locals>.<listcomp>r2   r   r   r   r   r   7   r   z$MultiPipelineCallbacks.tensor_inputsc                 C   s   | j D ]	}|||||}q|S )zr
        Calls all the callbacks in order with the given arguments and returns the final callback_kwargs.
        r2   )r   r   r    r&   r"   r4   r   r   r   r'   ;   s   
zMultiPipelineCallbacks.__call__N)r(   r)   r*   r+   r   r	   r   r-   r.   r   r   r   r'   r   r   r   r   r0   .   s    r0   c                   @   s,   e Zd ZdZdgZdeeef fd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           	      C   sd   | j j}| j j}|d ur|nt|j| }||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_stepr8   r   r   r   r#   O   s   zSDCFGCutoffCallback.callback_fnN	r(   r)   r*   r+   r   r   r.   r   r#   r   r   r   r   r7   E   s    r7   c                   @   s.   e Zd ZdZg dZdeeef fd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.
    )r8   add_text_embedsadd_time_idsr   c                 C   s   | j j}| j j}|d ur|nt|j| }||krX|| jd  }|dd  }|| jd  }	|	dd  }	|| jd  }
|
dd  }
d|_||| jd < |	|| jd < |
|| jd < |S )Nr   r9   r      r   r:   )r   r   r    r&   r"   r   r   r?   r8   rB   rC   r   r   r   r#   l   s    z!SDXLCFGCutoffCallback.callback_fnNr@   r   r   r   r   rA   b   s    rA   c                   @   s*   e Zd ZdZg Zdeeef fd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                 C   s@   | j j}| j j}|d ur|nt|j| }||kr|d |S )Nr   )r;   r   r   r<   r=   set_ip_adapter_scale)r   r   r    r&   r"   r   r   r?   r   r   r   r#      s   
z(IPAdapterScaleCutoffCallback.callback_fnNr@   r   r   r   r   rE      s    rE   N)typingr   r   r   configuration_utilsr   r   utilsr   r	   r0   r7   rA   rE   r   r   r   r   <module>   s    '%