
    6bi                     b   S r SSKrSSKJs  Jr  SSKJr  SSKJr  SSKJ	r	  SSKJ
r
  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  Sr\" S/ S9 " S S\\R>                  5      5       r \" S/ S9 " S S\\\R>                  5      5       r!S r"S r#S r$g)zLong Short-Term Memory layer.    N)activations)backend)constraints)initializers)regularizers)
base_layer)	InputSpec)gru_lstm_utils)	rnn_utils)RNN)DropoutRNNCellMixin)tf_utils)
tf_logging)keras_exportzbRNN `implementation=2` is not supported when `recurrent_dropout` is set. Using `implementation=1`.zkeras.layers.LSTMCell)v1c                      ^  \ rS rSrSr               SU 4S jjr\R                  U 4S j5       rS r	S r
SS jrU 4S jrSS	 jrS
rU =r$ )LSTMCell-   aw  Cell class for the LSTM layer.

See
[the TF-Keras RNN API guide](https://www.tensorflow.org/guide/tf_keras/rnn)
for details about the usage of RNN API.

This class processes one step within the whole time sequence input, whereas
`tf.keras.layer.LSTM` processes the whole sequence.

For example:

>>> inputs = tf.random.normal([32, 10, 8])
>>> rnn = tf.keras.layers.RNN(tf.keras.layers.LSTMCell(4))
>>> output = rnn(inputs)
>>> print(output.shape)
(32, 4)
>>> rnn = tf.keras.layers.RNN(
...    tf.keras.layers.LSTMCell(4),
...    return_sequences=True,
...    return_state=True)
>>> whole_seq_output, final_memory_state, final_carry_state = rnn(inputs)
>>> print(whole_seq_output.shape)
(32, 10, 4)
>>> print(final_memory_state.shape)
(32, 4)
>>> print(final_carry_state.shape)
(32, 4)

Args:
  units: Positive integer, dimensionality of the output space.
  activation: Activation function to use. Default: hyperbolic tangent
    (`tanh`). If you pass `None`, no activation is applied (ie. "linear"
    activation: `a(x) = x`).
  recurrent_activation: Activation function to use for the recurrent step.
    Default: sigmoid (`sigmoid`). If you pass `None`, no activation is
    applied (ie. "linear" activation: `a(x) = x`).
  use_bias: Boolean, (default `True`), whether the layer uses a bias vector.
  kernel_initializer: Initializer for the `kernel` weights matrix, used for
    the linear transformation of the inputs. Default: `glorot_uniform`.
  recurrent_initializer: Initializer for the `recurrent_kernel` weights
    matrix, used for the linear transformation of the recurrent state.
    Default: `orthogonal`.
  bias_initializer: Initializer for the bias vector. Default: `zeros`.
  unit_forget_bias: Boolean (default `True`). If True, add 1 to the bias of
    the forget gate at initialization. Setting it to true will also force
    `bias_initializer="zeros"`. This is recommended in [Jozefowicz et
      al.](https://github.com/mlresearch/v37/blob/gh-pages/jozefowicz15.pdf)
  kernel_regularizer: Regularizer function applied to the `kernel` weights
    matrix. Default: `None`.
  recurrent_regularizer: Regularizer function applied to
    the `recurrent_kernel` weights matrix. Default: `None`.
  bias_regularizer: Regularizer function applied to the bias vector.
    Default: `None`.
  kernel_constraint: Constraint function applied to the `kernel` weights
    matrix. Default: `None`.
  recurrent_constraint: Constraint function applied to the
    `recurrent_kernel` weights matrix. Default: `None`.
  bias_constraint: Constraint function applied to the bias vector. Default:
    `None`.
  dropout: Float between 0 and 1. Fraction of the units to drop for the
    linear transformation of the inputs. Default: 0.
  recurrent_dropout: Float between 0 and 1. Fraction of the units to drop
    for the linear transformation of the recurrent state. Default: 0.

Call arguments:
  inputs: A 2D tensor, with shape of `[batch, feature]`.
  states: List of 2 tensors that corresponding to the cell's units. Both of
    them have shape `[batch, units]`, the first tensor is the memory state
    from previous time step, the second tensor is the carry state from
    previous time step. For timestep 0, the initial state provided by user
    will be feed to cell.
  training: Python boolean indicating whether the layer should behave in
    training mode or in inference mode. Only relevant when `dropout` or
    `recurrent_dropout` is used.
c                   > US::  a  [        SU S35      e[        R                  R                  R	                  5       (       a  UR                  SS5      U l        OUR                  SS5      U l        [        TU ]   " S0 UD6  Xl	        [        R                  " U5      U l        [        R                  " U5      U l        X@l        [        R                  " U5      U l        [        R                  " U5      U l        [        R                  " U5      U l        Xl        [(        R                  " U	5      U l        [(        R                  " U
5      U l        [(        R                  " U5      U l        [0        R                  " U5      U l        [0        R                  " U5      U l        [0        R                  " U5      U l        [9        S[;        SU5      5      U l        [9        S[;        SU5      5      U l        UR                  S	S
5      nU R>                  S:w  a(  US:w  a"  [@        RB                  " [D        5        SU l#        OUU l#        U R                  U R                  /U l$        U R                  U l%        g )Nr   zQReceived an invalid value for argument `units`, expected a positive integer, got .enable_caching_deviceTF      ?        implementation       )&
ValueErrortfcompatr   #executing_eagerly_outside_functionspop_enable_caching_devicesuper__init__unitsr   get
activationrecurrent_activationuse_biasr   kernel_initializerrecurrent_initializerbias_initializerunit_forget_biasr   kernel_regularizerrecurrent_regularizerbias_regularizerr   kernel_constraintrecurrent_constraintbias_constraintminmaxdropoutrecurrent_dropoutloggingdebugRECURRENT_DROPOUT_WARNING_MSGr   
state_sizeoutput_size)selfr&   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r7   r8   kwargsr   	__class__s                      V/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/layers/rnn/lstm.pyr%   LSTMCell.__init__{   s   ( A:4497!= 
 99<<;;==*0**'+D' +1**'+D' 	"6"
%//*5$/OO4H$I! "."2"23E"F%1%5%56K%L" , 0 01A B 0"."2"23E"F%1%5%56K%L" , 0 01A B!,1B!C$/OO4H$I!*?3C 12!$S#c3D*E!F$4a8!!Q&>Q+>MM78"#D"0D::tzz2::    c           	        >^  [         TT ]  U5        [        R                  " T 5      nUS   nT R	                  UT R
                  S-  4ST R                  T R                  T R                  US9T l	        T R	                  T R
                  T R
                  S-  4ST R                  T R                  T R                  US9T l        T R                  (       a`  T R                  (       a  U 4S jnOT R                   nT R	                  T R
                  S-  4SUT R"                  T R$                  US9T l        OS T l        ST l        g )	N   kernel)shapenameinitializerregularizer
constraintcaching_devicerecurrent_kernelc           	      
  > [         R                  " TR                  " TR                  4/UQ70 UD6[        R
                  " S5      " TR                  4/UQ70 UD6TR                  " TR                  S-  4/UQ70 UD6/5      $ )Nonesr   )r   concatenater-   r&   r   r'   )_argsr?   r>   s      rA   r-   (LSTMCell.build.<locals>.bias_initializer   s    ".. 11!%048> ),,V4!%048> !11!%a 148<B
 rC   biasT)r$   buildr   rM   
add_weightr&   r+   r/   r2   rG   r,   r0   r3   rN   r*   r.   r-   r1   r4   rU   built)r>   input_shapedefault_caching_device	input_dimr-   r@   s   `    rA   rV   LSTMCell.build   s/   k"!*!9!9$!?O	oodjj1n-////--1 & 
 !%::tzzA~.#2222001 !0 !
 ==$$  $(#8#8 zzA~', 11//5 ( DI DI
rC   c                    Uu  pEpgUu  ppU R                  U[        R                  " XR                  SS2SU R                  24   5      -   5      nU R                  U[        R                  " XR                  SS2U R                  U R                  S-  24   5      -   5      nX-  XR                  U[        R                  " U
U R                  SS2U R                  S-  U R                  S-  24   5      -   5      -  -   nU R                  U[        R                  " XR                  SS2U R                  S-  S24   5      -   5      nX4$ )z.Computes carry and output using split kernels.Nr      )r)   r   dotrN   r&   r(   )r>   xh_tm1c_tm1x_ix_fx_cx_oh_tm1_ih_tm1_fh_tm1_ch_tm1_oifcos                   rA   _compute_carry_and_output"LSTMCell._compute_carry_and_output   s@   #-2*'%%'++g'<'<Q$**_'MNN
 %%kk..q$**tzzA~2M/MN
 IOOkk%%aa$**q.)H&HI
 
 
 %%kk'#8#8DJJN<L9L#MNO
 trC   c                     Uu  p4pVU R                  U5      nU R                  U5      nX-  XpR                  U5      -  -   n	U R                  U5      n
X4$ )z.Computes carry and output using fused kernels.)r)   r(   )r>   zrb   z0z1z2z3rk   rl   rm   rn   s              rA   _compute_carry_and_output_fused(LSTMCell._compute_carry_and_output_fused  sZ    %%b)%%b)IOOB///%%b)trC   c                    US   nUS   nU R                  XSS9nU R                  XCSS9nU R                  S:X  Ga  SU R                  s=:  a  S:  a   O  OXS   -  nXS   -  n	XS   -  n
XS   -  nOUnUn	Un
Un[        R
                  " U R                  SSS9u  pp[        R                  " X5      n[        R                  " X5      n[        R                  " X5      n[        R                  " X5      nU R                  (       a  [        R
                  " U R                  SSS9u  nnnn[        R                  " UU5      n[        R                  " UU5      n[        R                  " UU5      n[        R                  " UU5      nSU R                  s=:  a  S:  a   O  OXGS   -  nXGS   -  nXGS   -  nXGS   -  nOUnUnUnUnUUUU4nUUUU4nU R                  UXE5      u  nnOS	U R                  s=:  a  S:  a
  O  OXS   -  n[        R                  " XR                  5      nU[        R                  " X@R                  5      -  nU R                  (       a!  [        R                  " UU R                  5      n[        R
                  " USSS9nU R                  UU5      u  nnUU R!                  U5      -  n U U U/4$ )
Nr   r   rF   countr   r   r^   )num_or_size_splitsaxisr   )get_dropout_mask_for_cell#get_recurrent_dropout_mask_for_cellr   r7   r   splitrG   r   r_   r*   rU   bias_addr8   ro   rN   rw   r(   )!r>   inputsstatestrainingra   rb   dp_maskrec_dp_maskinputs_iinputs_finputs_cinputs_ok_ik_fk_ck_orc   rd   re   rf   b_ib_fb_cb_org   rh   ri   rj   r`   rm   rn   rr   hs!                                    rA   callLSTMCell.call  s   q	q	000K>>1 ? 
 !#4<<%#%!AJ.!AJ.!AJ.!AJ.!!!!!#"Cc ++h,C++h,C++h,C++h,C}}%'XXII!!&"S#s &&sC0&&sC0&&sC0&&sC04))/C/a.0a.0a.0a.0c3$Agw8E11!UBDAqT\\'C'!*,FKK0AU$9$9::A}}$$Q		2qq9A775ADAq""1a&yrC   c                 X  > 0 SU R                   _S[        R                  " U R                  5      _S[        R                  " U R                  5      _SU R
                  _S[        R                  " U R                  5      _S[        R                  " U R                  5      _S[        R                  " U R                  5      _SU R                  _S	[        R                  " U R                  5      _S
[        R                  " U R                  5      _S[        R                  " U R                  5      _S[        R                  " U R                   5      _S[        R                  " U R"                  5      _S[        R                  " U R$                  5      _SU R&                  _SU R(                  _SU R*                  _nUR-                  [.        R0                  " U 5      5        [2        TU ]i  5       n[7        [9        UR;                  5       5      [9        UR;                  5       5      -   5      $ )Nr&   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r7   r8   r   )r&   r   	serializer(   r)   r*   r   r+   r,   r-   r.   r   r/   r0   r1   r   r2   r3   r4   r7   r8   r   updater    config_for_enable_caching_devicer$   
get_configdictlistitemsr>   configbase_configr@   s      rA   r   LSTMCell.get_configS  s
   
TZZ
+//@
 #K$9$9))%
 
 !,"8"8''#
 $\%;%;**&
  6 6t7L7L M
  5 5
 !,"8"8''#
$ $\%;%;**&%
*  6 6t7L7L M+
,  !6!6t7M7M!N-
. #K$9$9))%/
4 {44T5I5IJ5
6 t||7
8  !7!79
: d11;
> 	i@@FGg(*D**,-V\\^0DDEErC   c                 B    [        [        R                  " XX#5      5      $ N)r   r   #generate_zero_filled_state_for_cell)r>   r   
batch_sizedtypes       rA   get_initial_stateLSTMCell.get_initial_statew  s"    99j
 	
rC   )r#   r(   rU   r4   r-   r1   rX   r7   r   rG   r2   r+   r/   r=   r)   r3   r8   r,   rN   r0   r<   r.   r&   r*   )tanhsigmoidTglorot_uniform
orthogonalzerosTNNNNNNr   r   r   NNN)__name__
__module____qualname____firstlineno____doc__r%   r   shape_type_conversionrV   ro   rw   r   r   r   __static_attributes____classcell__r@   s   @rA   r   r   -   s{    J^ &+* "!#>&@ ##3 $3j4=~"FH
 
rC   r   zkeras.layers.LSTMc                     ^  \ rS rSrSr                      SU 4S jjrSS jr\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rU 4S jr\S 5       rSrU =r$ )LSTMi  a  Long Short-Term Memory layer - Hochreiter 1997.

See
[the TF-Keras RNN API guide](https://www.tensorflow.org/guide/tf_keras/rnn)
for details about the usage of RNN API.

Based on available runtime hardware and constraints, this layer
will choose different implementations (cuDNN-based or pure-TensorFlow)
to maximize the performance. If a GPU is available and all
the arguments to the layer meet the requirement of the cuDNN kernel
(see below for details), the layer will use a fast cuDNN implementation.

The requirements to use the cuDNN implementation are:

1. `activation` == `tanh`
2. `recurrent_activation` == `sigmoid`
3. `recurrent_dropout` == 0
4. `unroll` is `False`
5. `use_bias` is `True`
6. Inputs, if use masking, are strictly right-padded.
7. Eager execution is enabled in the outermost context.

For example:

>>> inputs = tf.random.normal([32, 10, 8])
>>> lstm = tf.keras.layers.LSTM(4)
>>> output = lstm(inputs)
>>> print(output.shape)
(32, 4)
>>> lstm = tf.keras.layers.LSTM(4, return_sequences=True, return_state=True)
>>> whole_seq_output, final_memory_state, final_carry_state = lstm(inputs)
>>> print(whole_seq_output.shape)
(32, 10, 4)
>>> print(final_memory_state.shape)
(32, 4)
>>> print(final_carry_state.shape)
(32, 4)

Args:
  units: Positive integer, dimensionality of the output space.
  activation: Activation function to use.
    Default: hyperbolic tangent (`tanh`). If you pass `None`, no activation
    is applied (ie. "linear" activation: `a(x) = x`).
  recurrent_activation: Activation function to use for the recurrent step.
    Default: sigmoid (`sigmoid`). If you pass `None`, no activation is
    applied (ie. "linear" activation: `a(x) = x`).
  use_bias: Boolean (default `True`), whether the layer uses a bias vector.
  kernel_initializer: Initializer for the `kernel` weights matrix, used for
    the linear transformation of the inputs. Default: `glorot_uniform`.
  recurrent_initializer: Initializer for the `recurrent_kernel` weights
    matrix, used for the linear transformation of the recurrent state.
    Default: `orthogonal`.
  bias_initializer: Initializer for the bias vector. Default: `zeros`.
  unit_forget_bias: Boolean (default `True`). If True, add 1 to the bias of
    the forget gate at initialization. Setting it to true will also force
    `bias_initializer="zeros"`. This is recommended in [Jozefowicz et
        al.](http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf).
  kernel_regularizer: Regularizer function applied to the `kernel` weights
    matrix. Default: `None`.
  recurrent_regularizer: Regularizer function applied to the
    `recurrent_kernel` weights matrix. Default: `None`.
  bias_regularizer: Regularizer function applied to the bias vector.
    Default: `None`.
  activity_regularizer: Regularizer function applied to the output of the
    layer (its "activation"). Default: `None`.
  kernel_constraint: Constraint function applied to the `kernel` weights
    matrix. Default: `None`.
  recurrent_constraint: Constraint function applied to the
    `recurrent_kernel` weights matrix. Default: `None`.
  bias_constraint: Constraint function applied to the bias vector. Default:
    `None`.
  dropout: Float between 0 and 1. Fraction of the units to drop for the
    linear transformation of the inputs. Default: 0.
  recurrent_dropout: Float between 0 and 1. Fraction of the units to drop
    for the linear transformation of the recurrent state. Default: 0.
  return_sequences: Boolean. Whether to return the last output in the output
    sequence, or the full sequence. Default: `False`.
  return_state: Boolean. Whether to return the last state in addition to the
    output. Default: `False`.
  go_backwards: Boolean (default `False`). If True, process the input
    sequence backwards and return the reversed sequence.
  stateful: Boolean (default `False`). If True, the last state for each
  sample at index i in a batch will be used as initial state for the sample
    of index i in the following batch.
  time_major: The shape format of the `inputs` and `outputs` tensors.
    If True, the inputs and outputs will be in shape
    `[timesteps, batch, feature]`, whereas in the False case, it will be
    `[batch, timesteps, feature]`. Using `time_major = True` is a bit more
    efficient because it avoids transposes at the beginning and end of the
    RNN calculation. However, most TensorFlow data is batch-major, so by
    default this function accepts input and emits output in batch-major
    form.
  unroll: Boolean (default `False`). If True, the network will be unrolled,
    else a symbolic loop will be used. Unrolling can speed-up a RNN,
    although it tends to be more memory-intensive. Unrolling is only
    suitable for short sequences.

Call arguments:
  inputs: A 3D tensor with shape `[batch, timesteps, feature]`.
  mask: Binary tensor of shape `[batch, timesteps]` indicating whether
    a given timestep should be masked (optional).
    An individual `True` entry indicates that the corresponding timestep
    should be utilized, while a `False` entry indicates that the
    corresponding timestep should be ignored. Defaults to `None`.
  training: Python boolean indicating whether the layer should behave in
    training mode or in inference mode. This argument is passed to the cell
    when calling it. This is only relevant if `dropout` or
    `recurrent_dropout` is used (optional). Defaults to `None`.
  initial_state: List of initial state tensors to be passed to the first
    call of the cell (optional, `None` causes creation
    of zero-filled initial state tensors). Defaults to `None`.
c           
        > UR                  SS5      U l        UR                  SS5      nUS:X  a  [        R                  " S5        SU;   a  SUR                  S5      0nO0 n[	        U40 SU_S	U_S
U_SU_SU_SU_SU_SU	_SU
_SU_SU_SU_SU_SU_SU_SU_SUR                  S5      _SUR                  SS5      _SS_UD6n[        TU ]  " U4UUUUUUS.UD6  [        R
                  " U5      U l	        [        SS9/U l        U R                  U R                  4 Vs/ s H  n[        S U4S9PM     snU l        U R                  [        R                   ["        R                   4;   =(       a    U R$                  [        R&                  ["        R&                  4;   =(       aK    US:H  =(       a?    U(       + =(       a1    U=(       a(    ["        R(                  R*                  R-                  5       U l        ["        R0                  R3                  S 5      (       at  U R.                  (       a2  [        R4                  " [6        R8                  U R:                  -  5        O1[        R                  " [6        R<                  U R:                  -  5        [6        R>                  " 5       (       a  [6        R@                  " UUS!5      U l!        g g s  snf )"Nreturn_runtimeFr   r   r   zm`implementation=0` has been deprecated, and now defaults to `implementation=1`.Please update your layer call.r   r(   r)   r*   r+   r,   r.   r-   r/   r0   r1   r2   r3   r4   r7   r8   r   	trainableTrI   	lstm_cell)return_sequencesreturn_statego_backwardsstateful
time_majorunrollr^   )ndim)rH   GPUlstm)"r"   r   r9   warningr   r'   r$   r%   r   activity_regularizerr	   
input_specr&   
state_specr(   r   r   r   r)   r   r    r   r!   _could_use_gpu_kernelr   list_logical_devicesr:   r
   CUDNN_AVAILABLE_MSGrI   CUDNN_NOT_AVAILABLE_MSGuse_new_gru_lstm_implDefunWrapper_defun_wrapper)r>   r&   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r   r2   r3   r4   r7   r8   r   r   r   r   r   r   r?   r   cell_kwargscelldimr@   s                                rA   r%   LSTM.__init__  s   : %jj)95A$4a8QOO1
 #f,'4K)LK K
!
 "6
 	

  2
 #8
 .
 .
  2
 #8
 .
 0
 "6
 ,
 
  0!
" *#
$ **W%%
& jjd3'
( +
. 			
-%%!		
 		
 %1$4$45I$J!$!,-59ZZ4L
4LSIT3K(4L
 OO 0 0"'':: C))k.A.A2::-NNC!Q&C 
C 	C
 		@@B 	" 99))%00 ))n@@499LM"::TYYF //11"0"="=L&#D 2+
s   #K	c                 
  ^ ^ [         R                  " U5      u  pUS LnT R                  Xb5        T R                  XS 5      u  pn[	        U[
        5      (       a  US   n[         R                  " U5      nT R                  (       a  US   OUS   n	T R                  (       d  SU0mT R                  T R                  5        UU 4S jn
[         R                  " U
UUS T R                  UT R                  Ub  UOU	T R                  T R                  T R                  S9u  pn[         R"                  " [         R$                  5      nGOT R'                  5         T R)                  XSS9nUb  XS   -  n[         R*                  " 5       (       a  U[         R,                  " US   5      [         R,                  " US   5      [         R,                  " T R                  R.                  5      [         R,                  " T R                  R0                  5      [         R,                  " T R                  R2                  5      UT R                  T R                  UT R                  S.nT R4                  R6                  " S0 UD6u  nnnnnGOU[         R,                  " US   5      [         R,                  " US   5      [         R,                  " T R                  R.                  5      [         R,                  " T R                  R0                  5      [         R,                  " T R                  R2                  5      UT R                  T R                  UT R                  S	.nUR9                  5       nUR;                  S
T R                  05        [<        R>                  " 5       (       a  [         R@                  " 5       nU[         RB                  :H  =(       d*    US L =(       a    [<        RD                  RG                  S5      =(       a"    [         RH                  " UT R                  U5      nU(       a  [K        S0 UD6u  pnnnO#[M        S0 UD6u  nnnnnO[O        S0 UD6u  nnnnnUU/nT RP                  (       a  [S        T RT                  U5       VVs/ s HO  u  nn[<        RV                  RX                  R[                  U[<        R\                  " UUR^                  5      5      PMQ     nnnT Ra                  U5        T R                  (       a"  [         Rb                  " UUUT R                  S9nOUnT Rd                  (       a  U/[        U5      -   $ T Rf                  (       a  UU4$ U$ s  snnf )Nr   r   r   c                 *   > TR                   " X40 TD6$ r   )r   )r   r   r?   r>   s     rA   stepLSTM.call.<locals>.stepq  s    yy:6::rC   )	constantsr   maskr   input_lengthr   zero_output_for_maskreturn_all_outputsrF   rz   )r   init_hinit_crG   rN   rU   r   r   r   sequence_lengthsr   r   r   r   rG   rN   rU   r   r   r   r   r   r   r   )r   r   )4r   convert_inputs_if_ragged_validate_args_if_ragged_process_inputs
isinstancer   	int_shaper   r   _maybe_reset_cell_dropout_maskr   rnnr   r   r   r   r
   runtimeRUNTIME_UNKNOWNreset_dropout_maskr~   r   read_variable_valuerG   rN   rU   r   defun_layercopyr   r   executing_eagerlyget_context_device_typeGPU_DEVICE_NAMEr   r   is_cudnn_supported_inputsgpu_lstmstandard_lstmlstm_with_backend_selectionr   zipr   r    r   assigncastr   
add_updatemaybe_convert_to_raggedr   r   )r>   r   r   r   initial_staterow_lengthsis_ragged_inputrR   rY   	timestepsr   last_outputoutputsr   r   dropout_masklstm_kwargsnew_hnew_cgpu_lstm_kwargsnormal_lstm_kwargsdevice_typecan_use_gpu
self_statestateupdatesoutputr?   s   `                          @rA   r   	LSTM.callZ  s    &>>vF%T1%%o< $(#7#74$
 q dD!!7D''/&*ooKN;q>	)) (+F//		:; ,3;;!..{{* )??%)%>%>#'#8#8,(K& %,,^-K-KLG ##%99 : L 'q/13355$,@@%a( -@@%a( -@@		(( )7(J(J		22) +>>tyy~~N "&//$($5$5(3,0,E,E'6 ''33BkB %,@@%a( -@@%a( -@@		(( )7(J(J		22) +>>tyy~~N "&//$($5$5(3(,(=(='#* &5%9%9%;"")).0I0I ''))"0"H"H"JK
 (>+I+II  +t 3 !J$&II$B$B5$I		 +DD $//;    #FN G-GCeUG *?,>?'#!!# 4I6HI# U^F==
 *-T[[&)A	 *B%J 		##z/?/? @ *B	   OOG$  44!..	F !F8d6l**  7?"M/s   AUc                 .    U R                   R                  $ r   )r   r&   r>   s    rA   r&   
LSTM.units  s    yyrC   c                 .    U R                   R                  $ r   )r   r(   r  s    rA   r(   LSTM.activation  s    yy###rC   c                 .    U R                   R                  $ r   )r   r)   r  s    rA   r)   LSTM.recurrent_activation      yy---rC   c                 .    U R                   R                  $ r   )r   r*   r  s    rA   r*   LSTM.use_bias  s    yy!!!rC   c                 .    U R                   R                  $ r   )r   r+   r  s    rA   r+   LSTM.kernel_initializer      yy+++rC   c                 .    U R                   R                  $ r   )r   r,   r  s    rA   r,   LSTM.recurrent_initializer!      yy...rC   c                 .    U R                   R                  $ r   )r   r-   r  s    rA   r-   LSTM.bias_initializer%      yy)))rC   c                 .    U R                   R                  $ r   )r   r.   r  s    rA   r.   LSTM.unit_forget_bias)  r!  rC   c                 .    U R                   R                  $ r   )r   r/   r  s    rA   r/   LSTM.kernel_regularizer-  r  rC   c                 .    U R                   R                  $ r   )r   r0   r  s    rA   r0   LSTM.recurrent_regularizer1  r  rC   c                 .    U R                   R                  $ r   )r   r1   r  s    rA   r1   LSTM.bias_regularizer5  r!  rC   c                 .    U R                   R                  $ r   )r   r2   r  s    rA   r2   LSTM.kernel_constraint9      yy***rC   c                 .    U R                   R                  $ r   )r   r3   r  s    rA   r3   LSTM.recurrent_constraint=  r  rC   c                 .    U R                   R                  $ r   )r   r4   r  s    rA   r4   LSTM.bias_constraintA  s    yy(((rC   c                 .    U R                   R                  $ r   )r   r7   r  s    rA   r7   LSTM.dropoutE  s    yy   rC   c                 .    U R                   R                  $ r   )r   r8   r  s    rA   r8   LSTM.recurrent_dropoutI  r,  rC   c                 .    U R                   R                  $ r   )r   r   r  s    rA   r   LSTM.implementationM  s    yy'''rC   c                   > 0 SU R                   _S[        R                  " U R                  5      _S[        R                  " U R                  5      _SU R
                  _S[        R                  " U R                  5      _S[        R                  " U R                  5      _S[        R                  " U R                  5      _SU R                  _S	[        R                  " U R                  5      _S
[        R                  " U R                  5      _S[        R                  " U R                  5      _S[        R                  " U R                  5      _S[         R                  " U R"                  5      _S[         R                  " U R$                  5      _S[         R                  " U R&                  5      _SU R(                  _SU R*                  _SU R,                  0EnUR/                  [0        R2                  " U R4                  5      5        [6        TU ]q  5       nUS	 [;        [=        UR?                  5       5      [=        UR?                  5       5      -   5      $ )Nr&   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r   r2   r3   r4   r7   r8   r   r   ) r&   r   r   r(   r)   r*   r   r+   r,   r-   r.   r   r/   r0   r1   r   r   r2   r3   r4   r7   r8   r   r   r   r   r   r$   r   r   r   r   r   s      rA   r   LSTM.get_configQ  s9   !
TZZ!
+//@!
 #K$9$9))%!
 !
 !,"8"8''#!
 $\%;%;**&!
  6 6t7L7L M!
  5 5!
 !,"8"8''#!
$ $\%;%;**&%!
*  6 6t7L7L M+!
, #L$:$:))%-!
2  !6!6t7M7M!N3!
4 #K$9$9))%5!
: {44T5I5IJ;!
< t||=!
>  !7!7?!
@ d11A!
D 	i@@KLg(*D**,-V\\^0DDEErC   c                 :    SU;   a  US   S:X  a  SUS'   U " S0 UD6$ )Nr   r   r   r   r   )clsr   s     rA   from_configLSTM.from_configy  s.    v%&1A*Ba*G'(F#$}V}rC   )r   r   r   r   r   r   )r   r   Tr   r   r   TNNNNNNNr   r   FFFFFFr   )r   r   r   r   r   r%   r   propertyr&   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r7   r8   r   r   classmethodr;  r   r   r   s   @rA   r   r     s   oh &+* "!!1fPqf   $ $ . . " " , , / / * * * * , , / / * * + + . . ) ) ! ! + + ( (&FP  rC   r   c                   ^^^ [         R                  " U 5      nU(       a  US   OUS   nUUU4S jn[         R                  " UU X/SSUUUU	b  U	OUU
US9u  nnnUUUS   US   [        R                  " [        R
                  5      4$ )a	  LSTM with standard kernel implementation.

This implementation can be run on all types for hardware.

This implementation lifts out all the layer weights and make them function
parameters. It has same number of tensor input params as the cuDNN
counterpart. The RNN step logic has been simplified, eg dropout and mask is
removed since cuDNN implementation does not support that.

Note that the first half of the bias tensor should be ignored by this impl.
The cuDNN impl need an extra set of input gate bias. In order to make the
both function take same shape of parameter, that extra set of bias is also
feed
here.

Args:
  inputs: input tensor of LSTM layer.
  init_h: initial state tensor for the cell output.
  init_c: initial state tensor for the cell hidden state.
  kernel: weights for cell kernel.
  recurrent_kernel: weights for cell recurrent kernel.
  bias: weights for cell kernel bias and recurrent bias. Only recurrent bias
    is used in this case.
  mask: Boolean tensor for mask out the steps within sequence.
    An individual `True` entry indicates that the corresponding timestep
    should be utilized, while a `False` entry indicates that the
    corresponding timestep should be ignored.
  time_major: boolean, whether the inputs are in the format of
    [time, batch, feature] or [batch, time, feature].
  go_backwards: Boolean (default False). If True, process the input sequence
    backwards and return the reversed sequence.
  sequence_lengths: The lengths of all sequences coming from a variable
    length input, such as ragged tensors. If the input has a fixed timestep
    size, this should be None.
  zero_output_for_mask: Boolean, whether to output zero for masked timestep.
  return_sequences: Boolean. If True, return the recurrent outputs for all
    timesteps in the sequence. If False, only return the output for the
    last timestep (which consumes less memory).

Returns:
  last_output: output tensor for the last timestep, which has shape
    [batch, units].
  outputs:
    - If `return_sequences=True`: output tensor for all timesteps,
      which has shape [batch, time, units].
    - Else, a tensor equal to `last_output` with shape [batch, 1, units]
  state_0: the cell output, which has same shape as init_h.
  state_1: the cell hidden state, which has same shape as init_c.
  runtime: constant string tensor which indicate real runtime hardware. This
    value is for testing purpose and should be used by user.
r   r   c                   > US   nUS   n[         R                  " U T5      nU[         R                  " UT5      -  n[         R                  " UT5      n[        R                  " USSS9u  pVpx[        R
                  " U5      n	[        R
                  " U5      n
X-  U	[        R                  " U5      -  -   n[        R
                  " U5      nU[        R                  " U5      -  nXU/4$ )z8Step function that will be used by TF-Keras RNN backend.r   r   rF   r}   )r   r_   r   r   r   r   r   )cell_inputscell_statesra   rb   rr   rs   rt   ru   rv   rk   rl   rm   rn   r   rU   rG   rN   s                 rA   r   standard_lstm.<locals>.step  s    AAKKV,	W[[ 011Q%!QQ/JJrNJJrNIBGGBK'JJrN
Na&yrC   NF)r   r   r   r   r   r   r   r   )r   r   r   r
   r   RUNTIME_CPU)r   r   r   rG   rN   rU   r   r   r   r   r   r   rY   r   r   r   r  
new_statess      ```            rA   r   r     s    B ##F+K",A+a.I& (/{{	! 0 <)1+($K*  	11~99: rC   c                    Ub  [         R                  " Xg5      n	U(       d  U	c  [        R                  " U SS9n Su  pOU(       a  SOSu  p[        R                  " XS9n[        R                  " X+S9n[        R
                  " USSS9nU[        R
                  " USSS9-  n[        R                  " [        R                  " U5      U4S5      n[        R                  R                  5       S	   (       a>  S
 Vs/ s H  oU   PM	     nn[        R
                  " USSS9nS
 Vs/ s H  oU   PM	     nn[         R                  " U[        R
                  " US5      [        R                  " S/5      SS9nU	b}  U(       a  [        R                  " U U	UUS9n [        R                  R                  U UUUSSU	US9u  nnn  nU(       a-  [        R                  " UU	UUS9n[        R                  " UU/S9nODU(       a  [        R                  " U S/S9n [        R                  R!                  U UUUSSS9u  nnnnUS   nU(       d!  U	c  U
(       a  [        R                  " U/ SQS9n[        R"                  " UUS9n[        R"                  " UUS9nU	b  UnU
(       d  [        R                  " UU(       a  SOSS9nUUUU[         R$                  " [         R&                  5      4$ s  snf s  snf )a  LSTM with either cuDNN or ROCm implementation which is only available for
GPU.

Note that currently only right padded data is supported, or the result will
be polluted by the unmasked data which should be filtered.

Args:
  inputs: Input tensor of LSTM layer.
  init_h: Initial state tensor for the cell output.
  init_c: Initial state tensor for the cell hidden state.
  kernel: Weights for cell kernel.
  recurrent_kernel: Weights for cell recurrent kernel.
  bias: Weights for cell kernel bias and recurrent bias. Only recurrent bias
    is used in this case.
  mask: Boolean tensor for mask out the steps within sequence. An individual
    `True` entry indicates that the corresponding timestep should be
    utilized, while a `False` entry indicates that the corresponding
    timestep should be ignored.
  time_major: Boolean, whether the inputs are in the format of [time, batch,
    feature] or [batch, time, feature].
  go_backwards: Boolean (default False). If True, process the input sequence
    backwards and return the reversed sequence.
  sequence_lengths: The lengths of all sequences coming from a variable
    length input, such as ragged tensors. If the input has a fixed timestep
    size, this should be None.
  return_sequences: Boolean. If True, return the recurrent outputs for all
    timesteps in the sequence. If False, only return the output for the
    last timestep, matching the CPU function output format.

Returns:
  last_output: Output tensor for the last timestep, which has shape
    [batch, units].
  outputs:
    - If `return_sequences=True`: output tensor for all timesteps,
      which has shape [batch, time, units].
    - Else, a tensor equal to `last_output` with shape [batch, 1, units]
  state_0: The cell output, which has same shape as init_h.
  state_1: The cell hidden state, which has same shape as init_c.
  runtime: Constant string tensor which indicate real runtime hardware. This
    value is for testing purpose and should not be used by user.
)r   r   r   )perm)r   r   )r   r   rA  rF   r   r   is_rocm_build)r   r   r^   r   rF               rE   T)weightsbiasesrH   transpose_weights)seq_axis
batch_axisr   )inputinput_hinput_cparamsis_trainingrnn_moder   r   )rS  rT  rU  rV  rW  rX  )r
   calculate_sequence_by_maskr   	transposeexpand_dimsr   concat
zeros_like	sysconfigget_build_infocanonical_to_paramsconstantreverse_sequenceraw_ops
CudnnRNNV3reverseCudnnRNNsqueezer   RUNTIME_GPU)r   r   r   rG   rN   rU   r   r   r   r   r   rQ  rR  rN  	full_biasr`   rV  r  r   rm   rR   r   s                         rA   r   r     s   l )DD
 *2f95%*)3v ^^F2F^^F2Fhhvqq)Grxx(!!44G 		2==.5q9I	||""$_5 (@@'?!1:'?@HHY2	+CD+Caq\+C	D//xx	1%kk2$	F # (( !%	F !jj33-! 4 	
Aq! )) !%	G jjz:G
 ZZaS1F::.. / 
Aq "+K*27G,,wY7


18$A


18$A # ..
1J 			~99: ] A Es   9K"#K'c                 F  ^^ U UUUUUUUUU	U
US.mS m[         R                  " 5       (       aT  [        R                  R	                  [         R
                  U4S j[         R                  UU4S j0U4S j5      u  nnnnnOS[        [        R                  " 5       5      -   nUUS.n[         R                  " U[         R
                  [        U5      n[         R                  " U[         R                  TU5      nU" S0 TD6u  ppn[         R                  " U40 TD6  XXU4$ )	a=  Call the LSTM with optimized backend kernel selection.

Under the hood, this function will create two TF function, one with the most
generic kernel and can run on all device condition, and the second one with
cuDNN specific kernel, which can only run on GPU.

The first function will be called with normal_lstm_params, while the second
function is not called, but only registered in the graph. The Grappler will
do the proper graph rewrite and swap the optimized TF function based on the
device placement.

Args:
  inputs: Input tensor of LSTM layer.
  init_h: Initial state tensor for the cell output.
  init_c: Initial state tensor for the cell hidden state.
  kernel: Weights for cell kernel.
  recurrent_kernel: Weights for cell recurrent kernel.
  bias: Weights for cell kernel bias and recurrent bias. Only recurrent bias
    is used in this case.
  mask: Boolean tensor for mask out the steps within sequence.
    An individual `True` entry indicates that the corresponding timestep
    should be utilized, while a `False` entry indicates that the
    corresponding timestep should be ignored.
  time_major: Boolean, whether the inputs are in the format of
    [time, batch, feature] or [batch, time, feature].
  go_backwards: Boolean (default False). If True, process the input sequence
    backwards and return the reversed sequence.
  sequence_lengths: The lengths of all sequences coming from a variable
    length input, such as ragged tensors. If the input has a fixed timestep
    size, this should be None.
  zero_output_for_mask: Boolean, whether to output zero for masked timestep.
  return_sequences: Boolean. If True, return the recurrent outputs for all
    timesteps in the sequence. If False, only return the output for the
    last timestep (which consumes less memory).

Returns:
  List of output tensors, same as standard_lstm.
r   r   r   rG   rN   rU   r   r   r   r   r   r   c                    ^ ^^^^^^^^^	^
^ UUUUU UUUUU	U4S jnUUUUU UUUUU	UU
4S jn[         R                  R                  R                  [        R                  " TTT	5      UUS9$ )z<Use cuDNN kernel when mask is none or strictly right padded.c                  *   > [        TTTTTT TT
TT	TS9$ )Nr   )r   )rU   r   r   r   r   rG   r   rN   r   r   r   s   rA   cudnn_lstm_fnRlstm_with_backend_selection.<locals>.gpu_lstm_with_fallback.<locals>.cudnn_lstm_fn  s1    !1%)!1!1 rC   c                  ,   > [        TTTTTT TT
TT	TTS9$ )Nrk  r   )rU   r   r   r   r   rG   r   rN   r   r   r   r   s   rA   stardard_lstm_fnUlstm_with_backend_selection.<locals>.gpu_lstm_with_fallback.<locals>.stardard_lstm_fn  s4     !1%)!1%9!1 rC   )true_fnfalse_fn)r   __internal__
smart_condr
   r   )r   r   r   rG   rN   rU   r   r   r   r   r   r   rn  rr  s   ````````````  rA   gpu_lstm_with_fallback;lstm_with_backend_selection.<locals>.gpu_lstm_with_fallback  s_     	 		 	 	  ))4444j"2 "% 5 
 	
rC   c                     > [        S0 T D6$ Nr   rq  rV  s   rA   <lambda>-lstm_with_backend_selection.<locals>.<lambda>  s    8O8OrC   c                     > T " S0 TD6$ r{  r   )rx  rV  s   rA   r}  r~    s    8N 99rC   c                     > [        S0 T D6$ r{  rq  r|  s   rA   r}  r~  "  s    M+F+rC   lstm_)r   r   r   )r
   r   r   rv  execute_fn_for_deviceCPU_DEVICE_NAMEr   struuiduuid4generate_defun_backendr   function_register)r   r   r   rG   rN   rU   r   r   r   r   r   r   r   r  r  r  r   api_namesupportive_attributedefun_standard_lstmdefun_gpu_lstmrx  rV  s                        @@rA   r   r     s@   j , $, 4,F5
n ++-- OO11..0O.. 1 ,
	
 S..$( 
 -CC** 	
 (>>**" 	
 7J 7
7
3eG 	((B6Bw66rC   )%r   r  tensorflow.compat.v2r    v2r   tf_keras.srcr   r   r   r   r   tf_keras.src.enginer   tf_keras.src.engine.input_specr	   tf_keras.src.layers.rnnr
   r    tf_keras.src.layers.rnn.base_rnnr   .tf_keras.src.layers.rnn.dropout_rnn_cell_mixinr   tf_keras.src.utilsr   tensorflow.python.platformr   r9    tensorflow.python.util.tf_exportr   r;   BaseRandomLayerr   r   r   r   r   r   rC   rA   <module>r     s    $  ! ! $   $ % % * 4 2 - 0 N ' = 9   %"-N
"J$>$> N
 .N
b
 !b)}Z%?%? } *}@l^gTi7rC   