Ë
    (täi(  ã                   óÄ   — d dl 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é   )Ú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      €úb/Volumes/fast/ai/experiments/MLX_z-image/.venv/lib/python3.12/site-packages/diffusers/callbacks.pyr   zPipelineCallback.__init__   sz   ø€ ä‰ÑÔàÐ%Ð*;Ð*CØÐ)Ð.?Ð.KäÐrÓsÐsàÐ(ÜÐ,¬eÔ4¸SÐDUÔ=\Ð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   € ä!Ð$VÐW[×WeÑWeÐVfÐ"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   € Ü!Ð$WÐX\×XfÑXfÐWgÐ"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   ÚpropertyÚlistÚstrr   Údictr   r"   r&   Ú__classcell__)r   s   @r   r	   r	      sw   ø„ ñ	ð €KàôWó ðWð ði˜t C™yò ió ðiðjÈtÐTWÐY\ÐT\É~ó jðQÈ4ÐPSÐUXÐPXÉ>÷ 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-   € à&*§n¢nÔY¡n˜(À(×BXÔBX¸’ÐBX� nÒ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__;   s*   € ð ŸœˆHÙ& x°¸XÀÓW‰Oð 'ð Ðr   N)r'   r(   r)   r*   r-   r	   r   r,   r.   r   r/   r   r&   © r   r   r2   r2   .   sR   „ ñð
# $Ð'7Ñ"8ó #ð ðZ˜t C™yò Zó ðZðÈ4ÐPSÐUXÐPXÉ>ô 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“   € Ø ŸK™K×9Ñ9ÐØ ŸK™K×9Ñ9Ðð "3Ð!>ÑÄCÈ×H^ÑH^ÐarÑHrÓDsð 	ð ˜Ò$Ø+¨D×,>Ñ,>¸qÑ,AÑBˆMØ)¨"¨#Ð.ˆMà'*ˆHÔ$à5BˆO˜D×.Ñ.¨qÑ1Ñ2ØÐr   N©	r'   r(   r)   r*   r   r/   r.   r   r"   r:   r   r   r<   r<   E   s(   „ ñð %Ð%€MðÈdÐSVÐX[Ð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ÿ   € Ø ŸK™K×9Ñ9ÐØ ŸK™K×9Ñ9Ðð "3Ð!>ÑÄCÈ×H^ÑH^ÐarÑHrÓDsð 	ð ˜Ò$Ø+¨D×,>Ñ,>¸qÑ,AÑBˆMØ)¨"¨#Ð.ˆMà-¨d×.@Ñ.@ÀÑ.CÑDˆOØ-¨b¨cÐ2ˆOà*¨4×+=Ñ+=¸aÑ+@ÑAˆLØ'¨¨Ð,ˆLà'*ˆHÔ$à5BˆO˜D×.Ñ.¨qÑ1Ñ2Ø5DˆO˜D×.Ñ.¨qÑ1Ñ2Ø5AˆO˜D×.Ñ.¨qÑ1Ñ2àÐr   NrF   r:   r   r   rH   rH   b   s%   „ ñò€MðÈdÐSVÐX[Ð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  € Ø ŸK™K×9Ñ9ÐØ ŸK™K×9Ñ9Ðð "3Ð!>ÑÄCÈ×H^ÑH^ÐarÑHrÓDsð 	ð ˜Ò$Ø+¨D×,>Ñ,>¸qÑ,AÑBˆMØ)¨"¨#Ð.ˆMà-¨d×.@Ñ.@ÀÑ.CÑDˆOØ-¨b¨cÐ2ˆOà*¨4×+=Ñ+=¸aÑ+@ÑAˆLØ'¨¨Ð,ˆLð $ D×$6Ñ$6°qÑ$9Ñ:ˆEØ˜"˜#�JˆEà'*ˆHÔ$à5BˆO˜D×.Ñ.¨qÑ1Ñ2Ø5DˆO˜D×.Ñ.¨qÑ1Ñ2Ø5AˆO˜D×.Ñ.¨qÑ1Ñ2Ø5:ˆO˜D×.Ñ.¨qÑ1Ñ2àÐr   NrF   r:   r   r   rN   rN   Œ   s%   „ ñò€MðÈdÐSVÐX[Ð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   € Ø ŸK™K×9Ñ9ÐØ ŸK™K×9Ñ9Ðð "3Ð!>ÑÄCÈ×H^ÑH^ÐarÑHrÓDsð 	ð ˜Ò$Ø×)Ñ)¨#Ô.ØÐr   NrF   r:   r   r   rS   rS   ¼   s%   „ ñð €MðÈdÐSVÐX[Ð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Ð   € Ø ŸK™K×9Ñ9ÐØ ŸK™K×9Ñ9Ðð "3Ð!>ÑÄCÈ×H^ÑH^ÐarÑHrÓDsð 	ð ˜Ò$Ø+¨D×,>Ñ,>¸qÑ,AÑBˆMØ)¨"¨#Ð.ˆMà#2°4×3EÑ3EÀaÑ3HÑ#IÐ Ø#7Ø�ð$Ð ð (+ˆHÔ$à5BˆO˜D×.Ñ.¨qÑ1Ñ2Ø5IˆO˜D×.Ñ.¨qÑ1Ñ2ØÐr   NrF   r:   r   r   rW   rW   Ô   s+   „ ñð %Ð&<Ð=€MðÈdÐSVÐX[ÐS[Énô r   rW   N)Útypingr   Úconfiguration_utilsr   r   Úutilsr   r	   r2   r<   rH   rN   rS   rW   r:   r   r   Ú<module>r]      sj   ðÝ ç @Ý ô$Q�{ô $Q÷Nñ ô.Ð*ô ô:'Ð,ô 'ôT-Ð&6ô -ô`Ð#3ô ô0 Ð+õ  r   