U
    ¯Ê¹j  ã                   @   s¦  d Z ddlZddlmZ ddlmZ d2edœdd„ZG d	d
„ d
ejƒZ	d3edœdd„Z
G dd„ dejƒZd4edœdd„ZG dd„ dejƒZd5edœdd„ZG dd„ dejƒZd6edœdd„ZG dd„ dejƒZd7edœdd„ZG dd„ dejƒZd8edœdd „ZG d!d"„ d"ejƒZG d#d$„ d$ejƒZd9ejeejd%œd&d'„ZG d(d)„ d)ejƒZd:ejeejd%œd*d+„ZG d,d-„ d-ejƒZd;ejeejd%œd.d/„ZG d0d1„ d1ejƒZdS )<zÔ Activations

A collection of activations fn and modules with a common interface so that they can
easily be swapped. All have an `inplace` arg even if not used.

Hacked together by / Copyright 2020 Ross Wightman
é    N)Únn)Ú
functionalF©Úinplacec                 C   s    |r|   |  ¡ ¡S |  |  ¡ ¡S )z;Swish - Described in: https://arxiv.org/abs/1710.05941
    )Úmul_ÚsigmoidÚmul©Úxr   © r   úV/var/www/html/bwave/python/venv/lib/python3.8/site-packages/timm/layers/activations.pyÚswish   s    r   c                       s,   e Zd Zdedœ‡ fdd„Zdd„ Z‡  ZS )ÚSwishFr   c                    s   t t| ƒ ¡  || _d S ©N)Úsuperr   Ú__init__r   ©Úselfr   ©Ú	__class__r   r   r      s    zSwish.__init__c                 C   s   t || jƒS r   )r   r   ©r   r
   r   r   r   Úforward   s    zSwish.forward)F©Ú__name__Ú
__module__Ú__qualname__Úboolr   r   Ú__classcell__r   r   r   r   r      s   r   c                 C   s   |   t | ¡ ¡ ¡S )zšMish: A Self Regularized Non-Monotonic Neural Activation Function - https://arxiv.org/abs/1908.08681
    NOTE: I don't have a working inplace variant
    )r   ÚFZsoftplusÚtanhr	   r   r   r   Úmish   s    r    c                       s0   e Zd ZdZdedœ‡ fdd„Zdd„ Z‡  ZS )	ÚMishziMish: A Self Regularized Non-Monotonic Neural Activation Function - https://arxiv.org/abs/1908.08681
    Fr   c                    s   t t| ƒ ¡  d S r   )r   r!   r   r   r   r   r   r   '   s    zMish.__init__c                 C   s   t |ƒS r   )r    r   r   r   r   r   *   s    zMish.forward)F)r   r   r   Ú__doc__r   r   r   r   r   r   r   r   r!   $   s   r!   c                 C   s   |r|   ¡ S |  ¡ S r   )Úsigmoid_r   r	   r   r   r   r   .   s    r   c                       s,   e Zd Zdedœ‡ fdd„Zdd„ Z‡  ZS )ÚSigmoidFr   c                    s   t t| ƒ ¡  || _d S r   )r   r$   r   r   r   r   r   r   r   4   s    zSigmoid.__init__c                 C   s   | j r| ¡ S | ¡ S r   )r   r#   r   r   r   r   r   r   8   s    zSigmoid.forward)Fr   r   r   r   r   r$   3   s   r$   c                 C   s   |r|   ¡ S |  ¡ S r   )Útanh_r   r	   r   r   r   r   <   s    r   c                       s,   e Zd Zdedœ‡ fdd„Zdd„ Z‡  ZS )ÚTanhFr   c                    s   t t| ƒ ¡  || _d S r   )r   r&   r   r   r   r   r   r   r   B   s    zTanh.__init__c                 C   s   | j r| ¡ S | ¡ S r   )r   r%   r   r   r   r   r   r   F   s    zTanh.forward)Fr   r   r   r   r   r&   A   s   r&   c                 C   s,   t  | d ¡ d¡}|r"|  |¡S |  |¡S )Nç      @ç      @)r   Úrelu6Údiv_r   r   )r
   r   Úinnerr   r   r   Ú
hard_swishJ   s    r,   c                       s,   e Zd Zdedœ‡ fdd„Zdd„ Z‡  ZS )Ú	HardSwishFr   c                    s   t t| ƒ ¡  || _d S r   )r   r-   r   r   r   r   r   r   r   P   s    zHardSwish.__init__c                 C   s   t || jƒS r   )r,   r   r   r   r   r   r   T   s    zHardSwish.forward)Fr   r   r   r   r   r-   O   s   r-   c                 C   s2   |r|   d¡ dd¡ d¡S t | d ¡d S d S )Nr'   g        r(   )Zadd_Zclamp_r*   r   r)   r	   r   r   r   Úhard_sigmoidX   s    r.   c                       s,   e Zd Zdedœ‡ fdd„Zdd„ Z‡  ZS )ÚHardSigmoidFr   c                    s   t t| ƒ ¡  || _d S r   )r   r/   r   r   r   r   r   r   r   `   s    zHardSigmoid.__init__c                 C   s   t || jƒS r   )r.   r   r   r   r   r   r   d   s    zHardSigmoid.forward)Fr   r   r   r   r   r/   _   s   r/   c                 C   s>   |r |   d| d jddd� ¡S d|  | d jddd� S dS )z· Hard Mish
    Experimental, based on notes by Mish author Diganta Misra at
      https://github.com/digantamisra98/H-Mish/blob/0da20d4bc58e696b6803f2523c58d3c8a82782d0/README.md
    g      à?é   r   )ÚminÚmaxN)r   Úclampr	   r   r   r   Ú	hard_mishh   s    r4   c                       s,   e Zd Zdedœ‡ fdd„Zdd„ Z‡  ZS )ÚHardMishFr   c                    s   t t| ƒ ¡  || _d S r   )r   r5   r   r   r   r   r   r   r   t   s    zHardMish.__init__c                 C   s   t || jƒS r   )r4   r   r   r   r   r   r   x   s    zHardMish.forward)Fr   r   r   r   r   r5   s   s   r5   c                       sB   e Zd ZdZdeeeddœ‡ fdd„Zej	ej	d	œd
d„Z
‡  ZS )ÚPReLUz)Applies PReLU (w/ dummy inplace arg)
    é   ç      Ð?FN)Únum_parametersÚinitr   Úreturnc                    s   t t| ƒj||d� d S )N)r9   r:   )r   r6   r   )r   r9   r:   r   r   r   r   r      s    zPReLU.__init__©Úinputr;   c                 C   s   t  || j¡S r   )r   ZpreluÚweight©r   r=   r   r   r   r   ‚   s    zPReLU.forward)r7   r8   F)r   r   r   r"   ÚintÚfloatr   r   ÚtorchÚTensorr   r   r   r   r   r   r6   |   s   r6   )r
   r   r;   c                 C   s
   t  | ¡S r   ©r   Úgelur	   r   r   r   rE   †   s    rE   c                       s<   e Zd ZdZd	edœ‡ fdd„Zejejdœdd„Z‡  Z	S )
ÚGELUúLApplies the Gaussian Error Linear Units function (w/ dummy inplace arg)
    Fr   c                    s   t t| ƒ ¡  d S r   )r   rF   r   r   r   r   r   r   �   s    zGELU.__init__r<   c                 C   s
   t  |¡S r   rD   r?   r   r   r   r   �   s    zGELU.forward)F©
r   r   r   r"   r   r   rB   rC   r   r   r   r   r   r   rF   Š   s   rF   c                 C   s   t j| dd�S ©Nr   )ZapproximaterD   r	   r   r   r   Ú	gelu_tanh”   s    rJ   c                       s<   e Zd ZdZd	edœ‡ fdd„Zejejdœdd„Z‡  Z	S )
ÚGELUTanhrG   Fr   c                    s   t t| ƒ ¡  d S r   )r   rK   r   r   r   r   r   r   ›   s    zGELUTanh.__init__r<   c                 C   s   t j|dd�S rI   rD   r?   r   r   r   r   ž   s    zGELUTanh.forward)FrH   r   r   r   r   rK   ˜   s   rK   c                 C   s   | t  d|  ¡ S )Ng¬Zd;û?)rB   r   r	   r   r   r   Ú
quick_gelu¢   s    rL   c                       s<   e Zd ZdZd	edœ‡ fdd„Zejejdœdd„Z‡  Z	S )
Ú	QuickGELUrG   Fr   c                    s   t t| ƒ ¡  d S r   )r   rM   r   r   r   r   r   r   ©   s    zQuickGELU.__init__r<   c                 C   s   t |ƒS r   )rL   r?   r   r   r   r   ¬   s    zQuickGELU.forward)FrH   r   r   r   r   rM   ¦   s   rM   )F)F)F)F)F)F)F)F)F)F)r"   rB   r   Ztorch.nnr   r   r   r   ÚModuler   r    r!   r   r$   r   r&   r,   r-   r.   r/   r4   r5   r6   rC   rE   rF   rJ   rK   rL   rM   r   r   r   r   Ú<module>   s0   	
					


