
    bio                        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	                                      )zHelper 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    W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/models/activations.pyget_activationr   $   sO     \\^Fv  /x7TUYZaZfZfZhUiTj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(    vvfllne477EEr   )	__name__
__module____qualname____doc__r   torchTensorr'   __classcell__r   s   @r   r   r   5   s+    Fell 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*    IIfgD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66$'''6DDTDTUXX_c_i_iXjjvvd(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&    IIfg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66$'''67:::LLvvd|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   scalezThe `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    t9q=FJJw5A #Ugw(;<		-0!#&&}"!LQOO"/"5"5aR"5"@M4 499T?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    IIfg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"    IIfgD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'    IIaL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.    IIfgD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*    5s 5S 5 5QT 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  WWGGGGGGGGm3 m299 m"	Fryy 	F299 83BII 3B5RYY 5.,bii ,(	.ryy 	.r   