U
    9%e                     @   s   d dl Z d dlZd dlmZ dd Zdd Zdd Zd	d
 Zdd Z	dd Z
dddZeejjedkrdd ZejjjZeZneZeZee
eeeee	ejjjejjjejjjejjjejjjdZdd ZdS )    N)versionc              
   C   s:   t | } ddt j| t t d| j   }| | S )av  
    Gaussian Error Linear Unit. Original Implementation of the gelu activation function in Google Bert repo when
    initially created. For information: OpenAI GPT's gelu is slightly different (and gives slightly different results):
    0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) Also see
    https://arxiv.org/abs/1606.08415
          ?      ?       @)tfconvert_to_tensormatherfcastsqrtdtype)xcdf r   Z/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/activations_tf.py_gelu   s    
(r   c                 C   s`   t | } t tj| j}t d| j}ddt t d| | |t | d     }| | S )z
    Gaussian Error Linear Unit. This is a smoother version of the GELU. Original paper: https://arxiv.org/abs/1606.0841

    Args:
        x: float Tensor to perform activation

    Returns:
        `x` with the GELU activation applied.
    Hm?r   r   r      )	r   r   r
   r   pir   tanhr   pow)r   r   coeffr   r   r   r   	_gelu_new"   s
    

0r   c                 C   s    t | } | t t j|  S )N)r   r   r   r   Zsoftplusr   r   r   r   mish4   s    
r   c                 C   sP   t | } t d| j}t d| j}d|  dt | | d||  |      S )Nr   g3E?r   r   )r   r   r
   r   r   )r   Zcoeff1Zcoeff2r   r   r   	gelu_fast:   s    
r   c                 C   s,   t | } t d| j}| t j||   S )NgZd;?)r   r   r
   r   r   sigmoid)r   r   r   r   r   
quick_geluB   s    
r   c                 C   s   t t| ddS )a  
    Clip the range of possible GeLU outputs between [-10, 10]. This is especially useful for quantization purpose, as
    it allows mapping 2 negatives values in the GeLU spectrum. For more information on this trick, please refer to
    https://arxiv.org/abs/2004.09602

    Gaussian Error Linear Unit. Original Implementation of the gelu activation function in Google Bert repo when
    initially created. For information: OpenAI GPT's gelu is slightly different (and gives slightly different results):
    0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) Also see
    https://arxiv.org/abs/1606.08415 :param x: :return:
    i
   )r   Zclip_by_valuer   r   r   r   r   gelu_10H   s    r   c                 C   s$   t j| d|d\}}|t j| S )a  
    Gated Linear Unit. Implementation as defined in the original paper (see https://arxiv.org/abs/1612.08083), where
    the input `x` is split in two halves across a dimension (`axis`), A and B, returning A * sigmoid(B).

    Args:
        `x`: float Tensor to perform activation
        `axis`: dimension across which `x` be split in half

    Returns:
        `x` with the GLU activation applied (with its size halved across the dimension `axis`).
       )axis)r   splitr   r   )r   r"   abr   r   r   gluV   s    r&   z2.4c                 C   s   t jjj| ddS )NT)Zapproximate)r   kerasactivationsgelur   r   r   r   approximate_gelu_wraph   s    r*   )r)   r   r   gelu_newr&   r   r   relur   Zsiluswishr   c                 C   s0   | t krt |  S td|  dtt   d S )Nz	function z not found in ACT2FN mapping )ACT2FNKeyErrorlistkeys)Zactivation_stringr   r   r   get_tf_activation   s    r2   )r    )r   Z
tensorflowr   	packagingr   r   r   r   r   r   r   r&   parseVERSIONr*   r'   r(   r)   r+   r,   r   r-   r   r.   r2   r   r   r   r   <module>   s:   

