
    GJj?                     
   d dl Z d dlmZ d dlmZ d dlmZ d dlm	Z	 d Z
 eej                  d      d        Z eej                  d      d	        Z eej                  d      d
        Z eej                  d      d        Z eej                  d      d[d       Z eej                  d      d\d       Z eej                  d      d]d       Z eej                  d      d\d       Z eej                  d      d        Z eej                  d      d        Z eej                  d      d^defd       Z eej                  d      d]d       Z eej                  d      d        Z eej                  d      d        Z eej                  d      dej6                  fd       Z eej                  d      d        Z eej                  d      d        Zd\dej6                  dedej6                  fdZ  eej                  d      d_dej6                  defd       Z! eej                  d      d         Z" eej                  d      dej6                  d!ej6                  dej6                  fd"       Z# eej                  d      dej6                  dej6                  fd#       Z$ eej                  d      d$        Z% eej                  d      d`d%       Z& eej                  d      d^d&       Z' eej                  d      d\d'       Z(d( Z) G d) d*e	      Z* e
e       G d+ d,e	             Z+ e
e$       G d- d.e	             Z, e
e       G d/ d0e	             Z- e
e       G d1 d2e	             Z. e
e       G d3 d4e	             Z/ G d5 d6e	      Z0 G d7 d8e	      Z1 e
e       G d9 d:e	             Z2 e
e       G d; d<e	             Z3 e
e       G d= d>e	             Z4 G d? d@e	      Z5 G dA dBe	      Z6 e
e       G dC dDe	             Z7 e
e       G dE dFe	             Z8 e
e       G dG dHe	             Z9 G dI dJe	      Z: G dK dLe	      Z; e
e)       G dM dNe	             Z< e
e%       G dO dPe	             Z= G dQ dRe	      Z> e
e"       G dS dTe	             Z? e
e&       G dU dVe	             Z@ e
e'       G dW dXe	             ZA e
e(       G dY dZe	             ZBy)a    N)partial)Any)Modulec                       fd}|S )Nc                     fd| _         | S )Nc                      |      S N )_xfs     c/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/mlx/nn/layers/activations.py<lambda>z<_make_activation_module.<locals>.decorator.<locals>.<lambda>   s	    ad    )__call__)klassr   s    r   	decoratorz*_make_activation_module.<locals>.decorator   s    *r   r
   )r   r   s   ` r   _make_activation_moduler      s     r   T)	shapelessc                 ,    t        j                  |       S )zpApplies the sigmoid function.

    .. math::
        \text{Sigmoid}(x) = \sigma(x) = \frac{1}{1 + \exp(-x)}
    mxsigmoidr   s    r   r   r      s     ::a=r   c                 .    t        j                  | d      S )zIApplies the Rectified Linear Unit.

    Simply ``mx.maximum(x, 0)``.
    r   r   maximumr   s    r   relur      s     ::ar   c                 T    t        j                  t        j                  | d            S )u\   Applies the ReLU² activation function.

    Applies :math:`\max(0, x)^2` element wise.
    r   )r   squarer   r   s    r   relu2r!   &   s     99RZZ1%&&r   c                 V    t        j                  t        j                  | d      d      S )z`Applies the Rectified Linear Unit 6.

    Applies :math:`\min(\max(x, 0), 6)` element wise.
    r   g      @r   minimumr   r   s    r   relu6r%   /   s      ::bjjA&,,r   c                 4    t        j                  || z  |       S )z`Applies the Leaky Rectified Linear Unit.

    Simply ``mx.maximum(negative_slope * x, x)``.
    r   )r   negative_slopes     r   
leaky_relur(   8   s     ::nq(!,,r   c                 8    | t        j                  | |d      z
  S )zaApplies the Log Softmax function.

    Applies :math:`x + \log \sum_i e^{x_i}` element wise.
    T)axiskeepdims)r   	logsumexpr   r*   s     r   log_softmaxr.   A   s     r||AD4888r   c                 h    t        j                  | dkD  | |t        j                  |       dz
  z        S )zfApplies the Exponential Linear Unit.

    Simply ``mx.where(x > 0, x, alpha * (mx.exp(x) - 1))``.
    r      )r   whereexpr   alphas     r   elur5   J   s,     88AE1ervvay1}566r   c                 0    t        j                  | |      S )zdApplies the Softmax function.

    Applies :math:`\frac{e^{x_i}}{\sum_j e^{x_j}}` element wise.
    r*   r   softmaxr-   s     r   r9   r9   S   s     ::ad##r   c                 .    t        j                  | d      S )zXApplies the Softplus function.

    Applies :math:`\log(1 + \exp(x))` element wise.
    r   )r   	logaddexpr   s    r   softplusr<   \   s     <<1r   c                 Z    t        j                  | dt        j                  |       z         S )zXApplies the Softsign function.

    Applies :math:`\frac{x}{1 + |x|}` element wise.
    r0   )r   divideabsr   s    r   softsignr@   e   s!     99QBFF1I&&r   lambdc                     t        j                  t        j                  |       |kD  | t        j                  |       |z  z
  d      S )zApplies the Softshrink activation function.

    .. math::
        \text{softshrink}(x) = \begin{cases}
        x - \lambda & \text{if } x > \lambda \\
        x + \lambda & \text{if } x < -\lambda \\
        0 & \text{otherwise}
        \end{cases}
    r   )r   r1   r?   signr   rA   s     r   
softshrinkrE   n   s6     88BFF1I%q2771:+='=qAAr   c                     t        j                  | d      |t        j                  t        j                  | d      |z        dz
  z  z   S )zApplies the Continuously Differentiable Exponential Linear Unit.

    Applies :math:`\max(0, x) + \min(0, \alpha * (\exp(x / \alpha) - 1))`
    element wise.
            r0   )r   r   r2   r$   r3   s     r   celurH   |   s=     ::a

1c0BU0J)Ka)O PPPr   c                 2    | t        j                  |       z  S )zApplies the Sigmoid Linear Unit. Also known as Swish.

    Applies :math:`x \sigma(x)` element wise, where :math:`\sigma(\cdot)` is
    the logistic sigmoid.
    r   r   s    r   silurJ      s     rzz!}r   c                     t        |         S )zmApplies the Log Sigmoid function.

    Applies :math:`\log(\sigma(x)) = -\log(1 + e^{-x})` element wise.
    )r<   r   s    r   log_sigmoidrL      s     aRL=r   returnc                 j    | dt        j                  | t        j                  d      z        z   z  dz  S )zApplies the Gaussian Error Linear Units function.

    .. math::
        \textrm{GELU}(x) = x * \Phi(x)

    where :math:`\Phi(x)` is the Gaussian CDF.

    See also :func:`gelu_approx` and :func:`gelu_fast_approx` for faster
    approximations.
    r0      )r   erfmathsqrtr   s    r   gelurS      s/     BFF1tyy|+,,-11r   c           	          d| z  dt        j                  t        j                  dt        j                  z        | d| dz  z  z   z        z   z  S )at  An approximation to Gaussian Error Linear Unit.

    See :func:`gelu` for the exact computation.

    This function approximates ``gelu`` with a maximum absolute error :math:`<
    0.0005` in the range :math:`[-6, 6]` using the following

    .. math::

        x = 0.5 * x * \left(1 + \text{Tanh}\left((\sqrt{2 / \pi} * \left(x + 0.044715 * x^3\right)\right)\right)

          ?r0   rO   gHm?   )r   tanhrQ   rR   pir   s    r   gelu_approxrY      sD     7a"''$))AK"8A1a4<O"PQQRRr   c                 8    | t        j                  d| z        z  S )a  A fast approximation to Gaussian Error Linear Unit.

    See :func:`gelu` for the exact computation.

    This function approximates ``gelu`` with a maximum absolute error :math:`<
    0.015` in the range :math:`[-6, 6]` using the following

    .. math::

        x = x \sigma\left(1.702 x\right)

    where :math:`\sigma(\cdot)` is the logistic sigmoid.

    References:
    - https://github.com/hendrycks/GELUs
    - https://arxiv.org/abs/1606.08415
    gZd;?r   r   s    r   gelu_fast_approxr[      s    & rzz%!)$$$r   r   r*   c                 h    t        j                  | d|      \  }}|t        j                  |      z  S )?  Applies the gated linear unit function.

    This function splits the ``axis`` dimension of the input into two halves
    (:math:`a` and :math:`b`) and applies :math:`a * \sigma(b)`.

    .. math::
        \textrm{GLU}(x) = a * \sigma(b)

    Args:
        axis (int): The dimension to split along. Default: ``-1``
    rO   )indices_or_sectionsr*   )r   splitr   )r   r*   abs       r   glurb      s-     88A148DAqrzz!}r   	thresholdc                 6    t        j                  | |kD  dd      S )  Applies the Step Activation Function.

    This function implements a binary step activation, where the output is set
    to 1 if the input is greater than a specified threshold, and 0 otherwise.

    .. math::
        \text{step}(x) = \begin{cases}
        0 & \text{if } x < \text{threshold} \\
        1 & \text{if } x \geq \text{threshold}
        \end{cases}

    Args:
        threshold: The value to threshold at.
    r0   r   )r   r1   )r   rc   s     r   steprf      s    " 88A	M1a((r   c                      t        | d      dz  S )a:  Applies the Scaled Exponential Linear Unit.

    .. math::
        \text{selu}(x) = \begin{cases}
        \lambda x & \text{if } x > 0 \\
        \lambda \alpha (\exp(x) - 1) & \text{if } x \leq 0
        \end{cases}

    where :math:`\lambda = 1.0507` and :math:`\alpha = 1.67326`.

    See also :func:`elu`.
    gGG?g䃞ͪ?)r5   r   s    r   selurh      s     q'?V##r   r4   c                 b    t        j                  d|       |t        j                  d|       z  z   S )zApplies the element-wise parametric ReLU.

    .. math::
        \text{PReLU}(x) = \max(0,x) + a * \min(0,x)

    where :math:`a` is an array.
    r   r   r   r$   r3   s     r   prelurk     s*     ::aebjjA&6666r   c                 D    | t        j                  t        |             z  S )zApplies the Mish function, element-wise.

    Mish: A Self Regularized Non-Monotonic Neural Activation Function.

    Reference: https://arxiv.org/abs/1908.08681

    .. math::
        \text{Mish}(x) = x * \text{Tanh}(\text{Softplus}(x))

    )r   rW   r<   r   s    r   mishrm     s     rwwx{###r   c                 l    t        j                  | dz   d      }| t        j                  |d      z  dz  S )zApplies the hardswish function, element-wise.

    .. math::
        \text{Hardswish}(x) = x * \min(\max(x + 3, 0), 6) / 6
    rV   r      rj   )r   max_x_3s     r   	hardswishrq     s3     jjQ"Grzz'1%%))r   c                 V    t        j                  t        j                  | |      |      S )zzApplies the HardTanh function.

    Applies :math:`\max(\min(x, \mathrm{max\_val}), \mathrm{min\_val})` element-wise.
    r#   )r   min_valmax_vals      r   	hard_tanhru   *  s      ::bjjG,g66r   c                 \    t        j                  t        j                  |       |kD  | d      S )zApplies the HardShrink activation function.

    .. math::
        \text{hardshrink}(x) = \begin{cases}
        x & \text{if } x > \lambda \\
        x & \text{if } x < -\lambda \\
        0 & \text{otherwise}
        \end{cases}
    r   )r   r1   r?   rD   s     r   hard_shrinkrw   3  s$     88BFF1I%q!,,r   c                 2    t        j                  |  |      S )zfApplies the Softmin function.

    Applies :math:`\frac{e^{-x_i}}{\sum_j e^{-x_j}}` element-wise.
    r7   r8   r-   s     r   softminry   A  s     ::qbt$$r   c                 ,    t        j                  |       S )zIApplies the hyperbolic tangent function.

    Simply ``mx.tanh(x)``.
    )r   rW   r   s    r   rW   rW   J  s    
 771:r   c                   6     e Zd ZdZddef fdZdefdZ xZS )GLUr]   r*   c                 0    t         |           || _        y r	   )super__init__r*   )selfr*   	__class__s     r   r   zGLU.__init___  s    	r   rM   c                 0    t        || j                        S )Nr-   )rb   r*   r   r   s     r   r   zGLU.__call__c  s    QTYY''r   )	__name__
__module____qualname____doc__intr   r   r   __classcell__r   s   @r   r|   r|   R  s     
S (S (r   r|   c                       e Zd ZdZy)Sigmoidz~Applies the sigmoid function, element-wise.

    .. math::
        \text{Sigmoid}(x) = \sigma(x) = \frac{1}{1 + \exp(-x)}
    Nr   r   r   r   r
   r   r   r   r   g      r   r   c                       e Zd ZdZy)MishzApplies the Mish function, element-wise.

    Reference: https://arxiv.org/abs/1908.08681

    .. math::
        \text{Mish}(x) = x * \text{Tanh}(\text{Softplus}(x))

    Nr   r
   r   r   r   r   p  s    r   r   c                       e Zd ZdZy)ReLUzApplies the Rectified Linear Unit.
        Simply ``mx.maximum(x, 0)``.

    See :func:`relu` for the functional equivalent.
    Nr   r
   r   r   r   r   |  r   r   r   c                       e Zd ZdZy)ReLU2ub   Applies the ReLU² activation function.

    See :func:`relu2` for the functional equivalent.
    Nr   r
   r   r   r   r         r   r   c                       e Zd ZdZy)ReLU6z_Applies the Rectified Linear Unit 6.

    See :func:`relu6` for the functional equivalent.
    Nr   r
   r   r   r   r     r   r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )	LeakyReLUzApplies the Leaky Rectified Linear Unit.

    Simply ``mx.maximum(negative_slope * x, x)``.

    Args:
        negative_slope: Controls the angle of the negative slope. Default: ``1e-2``
    c                 0    t         |           || _        y r	   )r~   r   _negative_slope)r   r'   r   s     r   r   zLeakyReLU.__init__  s    -r   c                 .    t        || j                        S r	   )r(   r   r   s     r   r   zLeakyReLU.__call__  s    !T1122r   g{Gz?r   r   r   r   r   r   r   r   s   @r   r   r     s    .3r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )ELUzApplies the Exponential Linear Unit.
        Simply ``mx.where(x > 0, x, alpha * (mx.exp(x) - 1))``.

    See :func:`elu` for the functional equivalent.

    Args:
        alpha: the :math:`\alpha` value for the ELU formulation. Default: ``1.0``
    c                 0    t         |           || _        y r	   r~   r   _alphar   r4   r   s     r   r   zELU.__init__      r   c                 .    t        || j                        S r	   )r5   r   r   s     r   r   zELU.__call__  s    1dkk""r         ?r   r   s   @r   r   r     s    #r   r   c                       e Zd ZdZy)SoftmaxzZApplies the Softmax function.

    See :func:`softmax` for the functional equivalent.
    Nr   r
   r   r   r   r     r   r   r   c                       e Zd ZdZy)Softplusz\Applies the Softplus function.

    See :func:`softplus` for the functional equivalent.
    Nr   r
   r   r   r   r     r   r   r   c                       e Zd ZdZy)Softsignz\Applies the Softsign function.

    See :func:`softsign` for the functional equivalent.
    Nr   r
   r   r   r   r     r   r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )
SoftshrinkzApplies the Softshrink function.

    See :func:`softshrink` for the functional equivalent.

    Args:
        lambd: the :math:`\lambda` value for Softshrink. Default: ``0.5``
    c                 0    t         |           || _        y r	   )r~   r   rA   )r   rA   r   s     r   r   zSoftshrink.__init__  s    
r   c                 .    t        || j                        S r	   )rE   rA   r   s     r   r   zSoftshrink.__call__  s    !TZZ((r   rU   r   r   s   @r   r   r     s    )r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )CELUa<  Applies the Continuously Differentiable Exponential Linear Unit.
        Applies :math:`\max(0, x) + \min(0, \alpha * (\exp(x / \alpha) - 1))`
        element wise.

    See :func:`celu` for the functional equivalent.

    Args:
        alpha: the :math:`\alpha` value for the CELU formulation. Default: ``1.0``
    c                 0    t         |           || _        y r	   r   r   s     r   r   zCELU.__init__  r   r   c                 .    t        || j                        S r	   )rH   r   r   s     r   r   zCELU.__call__  s    At{{##r   r   r   r   s   @r   r   r     s    $r   r   c                       e Zd ZdZy)SiLUzoApplies the Sigmoid Linear Unit. Also known as Swish.

    See :func:`silu` for the functional equivalent.
    Nr   r
   r   r   r   r     r   r   r   c                       e Zd ZdZy)
LogSoftmaxzbApplies the Log Softmax function.

    See :func:`log_softmax` for the functional equivalent.
    Nr   r
   r   r   r   r     r   r   r   c                       e Zd ZdZy)
LogSigmoidzbApplies the Log Sigmoid function.

    See :func:`log_sigmoid` for the functional equivalent.
    Nr   r
   r   r   r   r     r   r   r   c                   D     e Zd ZdZd fd	Zdej                  fdZ xZS )PReLUa[  Applies the element-wise parametric ReLU.
        Applies :math:`\max(0, x) + a * \min(0, x)` element wise, where :math:`a`
        is an array.

    See :func:`prelu` for the functional equivalent.

    Args:
        num_parameters: number of :math:`a` to learn. Default: ``1``
        init: the initial value of :math:`a`. Default: ``0.25``
    c                 Z    t         |           t        j                  |g|      | _        y r	   )r~   r   r   fullweight)r   num_parametersinitr   s      r   r   zPReLU.__init__  s#    gg~.5r   r   c                 .    t        || j                        S r	   )rk   r   r   s     r   r   zPReLU.__call__  s    Q$$r   )r0   g      ?)	r   r   r   r   r   r   arrayr   r   r   s   @r   r   r     s    	6%"(( %r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )GELUa0  Applies the Gaussian Error Linear Units.

    .. math::
        \textrm{GELU}(x) = x * \Phi(x)

    where :math:`\Phi(x)` is the Gaussian CDF.

    However, if ``approx`` is set to 'precise' or 'fast' it applies

    .. math::
        \textrm{GELUApprox}(x) &= 0.5 * x * \left(1 + \text{Tanh}\left((\sqrt{2 / \pi} * \left(x + 0.044715 * x^3\right)\right)\right) \\
        \textrm{GELUFast}(x) &= x * \sigma\left(1.702 * x\right)

    respectively.

    .. note::
       For compatibility with the PyTorch API, 'tanh' can be used as an alias
       for 'precise'.

    See :func:`gelu`, :func:`gelu_approx` and :func:`gelu_fast_approx` for the
    functional equivalents and information regarding error bounds.


    Args:
        approx ('none' | 'precise' | 'fast'): Which approximation to gelu to use if any.
    c                 d    t         |           || _        g d}||vrt        d| d| d      y )N)nonepreciserW   fastzThe approximation should be in z but 'z' was given)r~   r   _approx
ValueError)r   approxallowedr   s      r   r   zGELU.__init__<  sF    5 1'&T  !r   c                 ~    | j                   dk(  rt        |      S | j                   dv rt        |      S t        |      S )Nr   )r   rW   )r   rS   rY   r[   r   s     r   r   zGELU.__call__E  s9    <<6!7N\\00q>!""r   )r   r   r   s   @r   r   r      s    6#r   r   c                       e Zd ZdZy)TanhzbApplies the hyperbolic tangent function.

    See :func:`tanh` for the functional equivalent.
    Nr   r
   r   r   r   r   M  r   r   r   c                       e Zd ZdZy)	HardswishzlApplies the hardswish function, element-wise.

    See :func:`hardswish` for the functional equivalent.
    Nr   r
   r   r   r   r   U  r   r   r   c                   J     e Zd ZdZddef fdZdej                  fdZ xZ	S )Stepre   rc   c                 0    t         |           || _        y r	   )r~   r   rc   )r   rc   r   s     r   r   zStep.__init__m  s    "r   r   c                 .    t        || j                        S r	   )rf   rc   r   s     r   r   zStep.__call__q  s    At~~&&r   rG   )
r   r   r   r   floatr   r   r   r   r   r   s   @r   r   r   ]  s$    #% #'"(( 'r   r   c                       e Zd ZdZy)SELUzeApplies the Scaled Exponential Linear Unit.

    See :func:`selu` for the functional equivalent.
    Nr   r
   r   r   r   r   u  r   r   r   c                       e Zd ZdZy)HardTanhz]Applies the HardTanh function.

    See :func:`hard_tanh` for the functional equivalent.
    Nr   r
   r   r   r   r   }  r   r   r   c                       e Zd ZdZy)
HardShrinkzApplies the HardShrink function.

    See :func:`hard_shrink` for the functional equivalent.

    Args:
        lambd: the :math:`\lambda` value for Hardshrink. Default: ``0.5``
    Nr   r
   r   r   r   r     s    r   r   c                       e Zd ZdZy)SoftminzZApplies the Softmin function.

    See :func:`softmin` for the functional equivalent.
    Nr   r
   r   r   r   r     r   r   r   r   r   r   r   r   )g      r   )CrQ   	functoolsr   typingr   mlx.corecorer   mlx.nn.layers.baser   r   compiler   r   r!   r%   r(   r.   r5   r9   r<   r@   r   rE   rH   rJ   rL   r   rS   rY   r[   r   rb   rf   rh   rk   rm   rq   ru   rw   ry   rW   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   <module>r      s,       % 	t$ % 	t$ % 	t$' %' 	t$- %- 	t$- %- 	t$9 %9 	t$7 %7 	t$$ %$ 	t$ % 	t$' %' 	t$
B 
B %
B 	t$Q %Q 	t$ % 	t$ % 	t$2rxx 2 %2 	t$S %S  	t$% %%*288 3    	t$)BHH ) ) %)& 	t$$ %$  	t$7RXX 7bhh 7288 7 %7 	t$$BHH $ $ %$ 	t$* %* 	t$7 %7 	t$
- %
- 	t$% %%(& (* !f  " 6   6   F    F   3 3"#& #$ !f  " "v  # "v  #) )"$6 $& 6   %  & %  &%F %(*#6 *#Z 6   #  $'6 '0 6   #v  $ %  & !f  "r   