Ë
    (täio  ã                   óð  — d dl Z d dlmc mZ d dl mZ ddlmZ ddlmZm	Z	  e«       rd dl
Z
ej                  ej                  ej                  ej                  ej                  dœZdedej"                  fd	„Z G d
„ dej"                  «      Z G d„ dej"                  «      Z G d„ dej"                  «      Z G d„ dej"                  «      Z G d„ dej"                  «      Z G d„ dej"                  «      Zy)é    N)Únné   )Ú	deprecate)Úis_torch_npu_availableÚis_torch_version)ÚswishÚsiluÚmishÚgeluÚreluÚact_fnÚreturnc           	      ó¤   — | j                  «       } | t        v rt        |    «       S t        d| › dt        t        j	                  «       «      › �«      ‚)z°Helper function to get activation function from string.

    Args:
        act_fn (str): Name of activation function.

    Returns:
        nn.Module: Activation function.
    zactivation function z not found in ACT2FN mapping )ÚlowerÚACT2CLSÚ
ValueErrorÚlistÚkeys)r   s    úk/Volumes/fast/ai/experiments/MLX_z-image/.venv/lib/python3.12/site-packages/diffusers/models/activations.pyÚget_activationr   $   sO   € ð �\‰\‹^€FØ”ÑÜ�v‰Ó Ð äÐ/°¨xÐ7TÔUYÔZa×ZfÑZfÓZhÓUiÐTjÐkÓlÐló    c                   óZ   ‡ — e Zd ZdZˆ fd„Zdej                  dej                  fd„Zˆ xZS )ÚFP32SiLUzH
    SiLU activation function with input upcasted to torch.float32.
    c                 ó"   •— t         ‰| �  «        y ©N)ÚsuperÚ__init__)ÚselfÚ	__class__s    €r   r   zFP32SiLU.__init__:   s   ø€ Ü‰ÑÕr   Úinputsr   c                 ó~   — t        j                  |j                  «       d¬«      j                  |j                  «      S )NF)Úinplace)ÚFr	   ÚfloatÚtoÚdtype)r   r    s     r   ÚforwardzFP32SiLU.forward=   s(   € Ü�v‰v�f—l‘l“n¨eÔ4×7Ñ7¸¿¹ÓEÐEr   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚtorchÚTensorr'   Ú__classcell__©r   s   @r   r   r   5   s+   ø„ ñôðF˜eŸl™lð F¨u¯|©|÷ Fr   r   c            	       ót   ‡ — e Zd ZdZddedededefˆ fd„Zdej                  dej                  fd	„Z
d
„ Zˆ xZS )ÚGELUa¢  
    GELU activation function with tanh approximation support with `approximate="tanh"`.

    Parameters:
        dim_in (`int`): The number of channels in the input.
        dim_out (`int`): The number of channels in the output.
        approximate (`str`, *optional*, defaults to `"none"`): If `"tanh"`, use tanh approximation.
        bias (`bool`, defaults to True): Whether to use a bias in the linear layer.
    Údim_inÚdim_outÚapproximateÚbiasc                 ój   •— t         ‰| �  «        t        j                  |||¬«      | _        || _        y ©N©r5   )r   r   r   ÚLinearÚprojr4   )r   r2   r3   r4   r5   r   s        €r   r   zGELU.__init__L   s*   ø€ Ü‰ÑÔÜ—I‘I˜f g°DÔ9ˆŒ	Ø&ˆÕr   Úgater   c                 ó@  — |j                   j                  dk(  ret        dd«      rYt        j                  |j                  t        j                  ¬«      | j                  ¬«      j                  |j                  ¬«      S t        j                  || j                  ¬«      S )NÚmpsÚ<ú2.0.0©r&   )r4   )
ÚdeviceÚtyper   r#   r   r%   r,   Úfloat32r4   r&   ©r   r;   s     r   r   z	GELU.geluQ   sp   € Ø�;‰;×Ñ˜uÒ$Ô)9¸#¸wÔ)Gä—6‘6˜$Ÿ'™'¬¯©˜'Ó6ÀD×DTÑDTÔU×XÑXÐ_c×_iÑ_iÐXÓjÐjÜ�v‰v�d¨×(8Ñ(8Ô9Ð9r   c                 óJ   — | j                  |«      }| j                  |«      }|S r   )r:   r   ©r   Úhidden_statess     r   r'   zGELU.forwardW   s$   € ØŸ	™	 -Ó0ˆØŸ	™	 -Ó0ˆØÐr   )ÚnoneT)r(   r)   r*   r+   ÚintÚstrÚboolr   r,   r-   r   r'   r.   r/   s   @r   r1   r1   A   sJ   ø„ ññ'˜sð '¨Sð '¸sð 'ÐSWõ 'ð
:˜Ÿ™ð :¨%¯,©,ó :ör   r1   c                   óp   ‡ — e Zd ZdZd
dededefˆ fd„Zdej                  dej                  fd„Z	d	„ Z
ˆ xZS )ÚGEGLUaN  
    A [variant](https://huggingface.co/papers/2002.05202) of the gated linear unit activation function.

    Parameters:
        dim_in (`int`): The number of channels in the input.
        dim_out (`int`): The number of channels in the output.
        bias (`bool`, defaults to True): Whether to use a bias in the linear layer.
    r2   r3   r5   c                 ób   •— t         ‰| �  «        t        j                  ||dz  |¬«      | _        y ©Nr   r8   ©r   r   r   r9   r:   ©r   r2   r3   r5   r   s       €r   r   zGEGLU.__init__g   s&   ø€ Ü‰ÑÔÜ—I‘I˜f g°¡k¸Ô=ˆ�	r   r;   r   c                 ó  — |j                   j                  dk(  rYt        dd«      rMt        j                  |j                  t        j                  ¬«      «      j                  |j                  ¬«      S t        j                  |«      S )Nr=   r>   r?   r@   )	rA   rB   r   r#   r   r%   r,   rC   r&   rD   s     r   r   z
GEGLU.geluk   s\   € Ø�;‰;×Ñ˜uÒ$Ô)9¸#¸wÔ)Gä—6‘6˜$Ÿ'™'¬¯©˜'Ó6Ó7×:Ñ:ÀÇÁÐ:ÓLÐLÜ�v‰v�d‹|Ðr   c                 ó   — t        |«      dkD  s|j                  dd «      �d}t        dd|«       | j                  |«      }t	        «       rt        j                  |dd¬«      d   S |j                  dd¬	«      \  }}|| j                  |«      z  S )
Nr   ÚscalezðThe `scale` argument is deprecated and will be ignored. Please remove it, as passing it will raise an error in the future. `scale` should directly be passed while calling the underlying pipeline component i.e., via `cross_attention_kwargs`.z1.0.0éÿÿÿÿé   )Údimr4   r   ©rW   )	ÚlenÚgetr   r:   r   Ú	torch_npuÚ	npu_gegluÚchunkr   )r   rG   ÚargsÚkwargsÚdeprecation_messager;   s         r   r'   zGEGLU.forwardq   sŽ   € Üˆt‹9�qŠ=˜FŸJ™J w°Ó5ÐAð #UÐÜ�g˜wÐ(;Ô<ØŸ	™	 -Ó0ˆÜ!Ô#ä×&Ñ& }¸"È!ÔLÈQÑOÐOà"/×"5Ñ"5°a¸RÐ"5Ó"@ÑˆM˜4Ø  4§9¡9¨T£?Ñ2Ð2r   ©T)r(   r)   r*   r+   rI   rK   r   r,   r-   r   r'   r.   r/   s   @r   rM   rM   ]   sB   ø„ ññ>˜sð >¨Sð >¸õ >ð˜Ÿ™ð ¨%¯,©,ó ö
3r   rM   c                   ó8   ‡ — e Zd ZdZddededefˆ fd„Zd„ Zˆ xZS )ÚSwiGLUa‘  
    A [variant](https://huggingface.co/papers/2002.05202) of the gated linear unit activation function. It's similar to
    `GEGLU` but uses SiLU / Swish instead of GeLU.

    Parameters:
        dim_in (`int`): The number of channels in the input.
        dim_out (`int`): The number of channels in the output.
        bias (`bool`, defaults to True): Whether to use a bias in the linear layer.
    r2   r3   r5   c                 ó”   •— t         ‰| �  «        t        j                  ||dz  |¬«      | _        t        j
                  «       | _        y rO   )r   r   r   r9   r:   ÚSiLUÚ
activationrQ   s       €r   r   zSwiGLU.__init__‰   s3   ø€ Ü‰ÑÔä—I‘I˜f g°¡k¸Ô=ˆŒ	ÜŸ'™'›)ˆ�r   c                 óx   — | j                  |«      }|j                  dd¬«      \  }}|| j                  |«      z  S )Nr   rU   rX   )r:   r]   rf   )r   rG   r;   s      r   r'   zSwiGLU.forward�   s>   € ØŸ	™	 -Ó0ˆØ+×1Ñ1°!¸Ð1Ó<Ñˆ�tØ˜tŸ™¨tÓ4Ñ4Ð4r   ra   )	r(   r)   r*   r+   rI   rK   r   r'   r.   r/   s   @r   rc   rc   ~   s'   ø„ ññ$˜sð $¨Sð $¸õ $ö5r   rc   c                   ój   ‡ — e Zd ZdZd	dededefˆ fd„Zdej                  dej                  fd„Z	ˆ xZ
S )
ÚApproximateGELUaˆ  
    The approximate form of the Gaussian Error Linear Unit (GELU). For more details, see section 2 of this
    [paper](https://huggingface.co/papers/1606.08415).

    Parameters:
        dim_in (`int`): The number of channels in the input.
        dim_out (`int`): The number of channels in the output.
        bias (`bool`, defaults to True): Whether to use a bias in the linear layer.
    r2   r3   r5   c                 ó\   •— t         ‰| �  «        t        j                  |||¬«      | _        y r7   rP   rQ   s       €r   r   zApproximateGELU.__init__    s"   ø€ Ü‰ÑÔÜ—I‘I˜f g°DÔ9ˆ�	r   Úxr   c                 óZ   — | j                  |«      }|t        j                  d|z  «      z  S )Ng¬Zd;û?)r:   r,   Úsigmoid)r   rk   s     r   r'   zApproximateGELU.forward¤   s'   € Ø�I‰I�a‹LˆØ”5—=‘= ¨¡Ó+Ñ+Ð+r   ra   )r(   r)   r*   r+   rI   rK   r   r,   r-   r'   r.   r/   s   @r   ri   ri   •   s=   ø„ ññ:˜sð :¨Sð :¸õ :ð,˜Ÿ™ð ,¨%¯,©,÷ ,r   ri   c            	       ó8   ‡ — e Zd Zddedededefˆ fd„Zd„ Zˆ xZS )ÚLinearActivationr2   r3   r5   rf   c                 ó|   •— t         ‰| �  «        t        j                  |||¬«      | _        t        |«      | _        y r7   )r   r   r   r9   r:   r   rf   )r   r2   r3   r5   rf   r   s        €r   r   zLinearActivation.__init__ª   s.   ø€ Ü‰ÑÔä—I‘I˜f g°DÔ9ˆŒ	Ü(¨Ó4ˆ�r   c                 óF   — | j                  |«      }| j                  |«      S r   )r:   rf   rF   s     r   r'   zLinearActivation.forward°   s   € ØŸ	™	 -Ó0ˆØ�‰˜}Ó-Ð-r   )Tr	   )	r(   r)   r*   rI   rK   rJ   r   r'   r.   r/   s   @r   ro   ro   ©   s*   ø„ ñ5˜sð 5¨Sð 5¸ð 5ÐQTõ 5ö.r   ro   )r,   Útorch.nn.functionalr   Ú
functionalr#   Úutilsr   Úutils.import_utilsr   r   r[   re   ÚMishr1   ÚReLUr   rJ   ÚModuler   r   rM   rc   ri   ro   © r   r   Ú<module>rz      sÍ   ðó  ß Ð Ý å ß Iñ ÔÛð �W‰WØ�G‰GØ�G‰GØ�G‰GØ�G‰Gñ€ðm˜3ð m 2§9¡9ó mô"	Fˆr�y‰yô 	Fôˆ2�9‰9ô ô83ˆB�I‰Iô 3ôB5ˆR�Y‰Yô 5ô.,�b—i‘iô ,ô(	.�r—y‘yõ 	.r   