
    j"                         d dl Z d dlmZmZ d dlmZ d dlmZ d dl	m
Z
  G d de
          Z G d de
          Z G d	 d
e
          ZdS )    N)CallableOptional)tanh)Modulec            
       T     e Zd ZdZ	 	 ddedededee         f fdZd	 Z	dd
Z
 xZS )RNNa  An Elman recurrent layer.

    The input is a sequence of shape ``NLD`` or ``LD`` where:

    * ``N`` is the optional batch dimension
    * ``L`` is the sequence length
    * ``D`` is the input's feature dimension

    Concretely, for each element along the sequence length axis, this
    layer applies the function:

    .. math::

        h_{t + 1} = \text{tanh} (W_{ih}x_t + W_{hh}h_t + b)

    The hidden state :math:`h` has shape ``NH`` or ``H``, depending on
    whether the input is batched or not. Returns the hidden state at each
    time step, of shape ``NLH`` or ``LH``.

    Args:
        input_size (int): Dimension of the input, ``D``.
        hidden_size (int): Dimension of the hidden state, ``H``.
        bias (bool, optional): Whether to use a bias. Default: ``True``.
        nonlinearity (callable, optional): Non-linearity to use. If ``None``,
            then func:`tanh` is used. Default: ``None``.
    TN
input_sizehidden_sizebiasnonlinearityc                    t                                                       |pt          | _        t	          | j                  st          d| d          dt          j        |          z  }|| _        t          j
                            | |||f          | _        t          j
                            | |||f          | _        |r$t          j
                            | ||f          nd | _        d S )Nz.Nonlinearity must be callable. Current value: .      ?lowhighshape)super__init__r   r   callable
ValueErrormathsqrtr
   mxrandomuniformWxhWhhr   )selfr	   r
   r   r   scale	__class__s         a/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/mlx/nn/layers/recurrent.pyr   zRNN.__init__'   s    	(0D)** 	PPPP   di,,,&9$$U;
*C % 
 
 9$$U;*D % 
 

 BI5&u[NKKK 				    c           	      `    d| j         j        d          d| j         d| j         d| j        d u S )Ninput_dims=   , hidden_size=z, nonlinearity=, bias=)r   r   r
   r   r   r   s    r"   _extra_reprzRNN._extra_reprD   s^    N$(.+ N N+N N -N N6:it6KN N	
r#   c                    | j         &t          j        | j         || j        j                  }n|| j        j        z  }g }t          |j        d                   D ]g}|,t          j        |d|d d f         || j        j                  }n|d|d d f         }|                     |          }|	                    |           ht          j
        |d          S )N.axis)r   r   addmmr   Tranger   r   r   appendstack)r   xhidden
all_hiddenidxs        r"   __call__zRNN.__call__K   s    9 Atxz22AADHJA
%% 	& 	&C!!CaaaK.&$(*EE3QQQ;&&v..Ff%%%%x
,,,,r#   )TNN)__name__
__module____qualname____doc__intboolr   r   r   r*   r8   __classcell__r!   s   @r"   r   r      s         > +/
 

 
 	

 x(
 
 
 
 
 
:
 
 
- - - - - - - -r#   r   c                   B     e Zd ZdZ	 d
dededef fdZd Zdd	Z xZ	S )GRUa  A gated recurrent unit (GRU) RNN layer.

    The input has shape ``NLD`` or ``LD`` where:

    * ``N`` is the optional batch dimension
    * ``L`` is the sequence length
    * ``D`` is the input's feature dimension

    Concretely, for each element of the sequence, this layer computes:

    .. math::

        \begin{aligned}
        r_t &= \sigma (W_{xr}x_t + W_{hr}h_t + b_{r}) \\
        z_t &= \sigma (W_{xz}x_t + W_{hz}h_t + b_{z}) \\
        n_t &= \text{tanh}(W_{xn}x_t + b_{n} + r_t \odot (W_{hn}h_t + b_{hn})) \\
        h_{t + 1} &= (1 - z_t) \odot n_t + z_t \odot h_t
        \end{aligned}

    The hidden state :math:`h` has shape ``NH`` or ``H`` depending on
    whether the input is batched or not. Returns the hidden state at each
    time step of shape ``NLH`` or ``LH``.

    Args:
        input_size (int): Dimension of the input, ``D``.
        hidden_size (int): Dimension of the hidden state, ``H``.
        bias (bool): Whether to use biases or not. Default: ``True``.
    Tr	   r
   r   c                    t                                                       || _        dt          j        |          z  }t
          j                            | |d|z  |f          | _        t
          j                            | |d|z  |f          | _	        |r't
          j                            | |d|z  f          nd | _
        |r$t
          j                            | ||f          nd | _        d S )Nr      r   )r   r   r
   r   r   r   r   r   WxWhbbhnr   r	   r
   r   r    r!   s        r"   r   zGRU.__init__{   s
    	&di,,,)##U1{?J*G $ 
 
 )##U1{?K*H $ 
 

 BI5&uQ_<NOOO 	 BI5&u[NKKK 	r#   c                 P    d| j         j        d          d| j         d| j        d u S Nr%   r&   r'   r(   )rF   r   r
   rH   r)   s    r"   r*   zGRU._extra_repr   sL    I$'-* I I+I I48F$4FI I	
r#   Nc                    | j         &t          j        | j         || j        j                  }n|| j        j        z  }|dd | j         f         }|d| j         d f         }g }t          |j        d                   D ]}|d|d d f         }|I|| j        j        z  }|dd | j         f         }	|d| j         d f         }
| j	        
|
| j	        z  }
||	z   }t          j
        |          }t          j        |dd          \  }}|d|d d f         }||||
z  z   }t          j        |          }|d|z
  |z  ||z  z   }nd|z
  |z  }|                    |           t          j        |d          S )N.r,      r-   r&   )rH   r   r/   rF   r0   r
   r1   r   rG   rI   sigmoidsplitr   r2   r3   )r   r4   r5   x_rzx_nr6   r7   rzh_proj	h_proj_rzh_proj_nrzns                 r"   r8   zGRU.__call__   s   6DGI..AADGIA)))))*d&&((()
%% 	& 	&Cc3k"B!$')+"3(;4+;*;(;#;<	!#(8'8':':":;8'(H)^BB8B+++DAqCaaaK A!H$

A!a%1q6z1a%1f%%%%x
,,,,r#   Tr9   
r:   r;   r<   r=   r>   r?   r   r*   r8   r@   rA   s   @r"   rC   rC   ]   s         B 	
 

 
 	
 
 
 
 
 
6
 
 
(- (- (- (- (- (- (- (-r#   rC   c                   B     e Zd ZdZ	 d
dededef fdZd Zdd	Z xZ	S )LSTMau  An LSTM recurrent layer.

    The input has shape ``NLD`` or ``LD`` where:

    * ``N`` is the optional batch dimension
    * ``L`` is the sequence length
    * ``D`` is the input's feature dimension

    Concretely, for each element of the sequence, this layer computes:

    .. math::
        \begin{aligned}
        i_t &= \sigma (W_{xi}x_t + W_{hi}h_t + b_{i}) \\
        f_t &= \sigma (W_{xf}x_t + W_{hf}h_t + b_{f}) \\
        g_t &= \text{tanh} (W_{xg}x_t + W_{hg}h_t + b_{g}) \\
        o_t &= \sigma (W_{xo}x_t + W_{ho}h_t + b_{o}) \\
        c_{t + 1} &= f_t \odot c_t + i_t \odot g_t \\
        h_{t + 1} &= o_t \text{tanh}(c_{t + 1})
        \end{aligned}

    The hidden state :math:`h` and cell state :math:`c` have shape ``NH``
    or ``H``, depending on whether the input is batched or not.

    The layer returns two arrays, the hidden state and the cell state at
    each time step, both of shape ``NLH`` or ``LH``.

    Args:
        input_size (int): Dimension of the input, ``D``.
        hidden_size (int): Dimension of the hidden state, ``H``.
        bias (bool): Whether to use biases or not. Default: ``True``.
    Tr	   r
   r   c                    t                                                       || _        dt          j        |          z  }t
          j                            | |d|z  |f          | _        t
          j                            | |d|z  |f          | _	        |r't
          j                            | |d|z  f          nd | _
        d S )Nr      r   )r   r   r
   r   r   r   r   r   rF   rG   r   rJ   s        r"   r   zLSTM.__init__   s     	&di,,,)##U1{?J*G $ 
 
 )##U1{?K*H $ 
 

 BI5&uQ_<NOOO 				r#   c                 P    d| j         j        d          d| j         d| j        d u S rL   )rF   r   r
   r   r)   s    r"   r*   zLSTM._extra_repr   sL    L$'-* L L+L L48IT4IL L	
r#   Nc                    | j         &t          j        | j         || j        j                  }n|| j        j        z  }g }g }t          |j        d                   D ]}|d|d d f         }| t          j        ||| j        j                  }t          j        |dd          \  }}	}
}t          j	        |          }t          j	        |	          }	t          j
        |
          }
t          j	        |          }||	|z  ||
z  z   }n||
z  }|t          j
        |          z  }|                    |           |                    |           t          j        |d          t          j        |d          fS )Nr,   .r`   rO   r-   )r   r   r/   rF   r0   r1   r   rG   rQ   rP   r   r2   r3   )r   r4   r5   cellr6   all_cellr7   ifgoifgos               r"   r8   zLSTM.__call__  sb   9 Atwy11AADGIA
%% 	& 	&CS#qqq[>D!xfdgi88$333JAq!Q
1A
1A

A
1A4x!a%'1u&FOOD!!!f%%%%x
,,,bhxb.I.I.IIIr#   r[   )NNr\   rA   s   @r"   r^   r^      s         H 	
 

 
 	
 
 
 
 
 
,
 
 
J J J J J J J Jr#   r^   )r   typingr   r   mlx.corecorer   mlx.nn.layers.activationsr   mlx.nn.layers.baser   r   rC   r^    r#   r"   <module>rp      s    % % % % % % % %       * * * * * * % % % % % %O- O- O- O- O-& O- O- O-dg- g- g- g- g-& g- g- g-TZJ ZJ ZJ ZJ ZJ6 ZJ ZJ ZJ ZJ ZJr#   