
    6bi'                         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
9 " S S\5      5       rg)z Fast LSTM layer backed by cuDNN.    N)constraints)initializers)regularizers)gru_lstm_utils)	_CuDNNRNN)keras_exportzkeras.layers.CuDNNLSTM)v1c                   |   ^  \ rS rSrSr               S	U 4S jjr\S 5       rU 4S jrS r	U 4S jr
SrU =r$ )
	CuDNNLSTM    a  Fast LSTM implementation backed by cuDNN.

More information about cuDNN can be found on the [NVIDIA
developer website](https://developer.nvidia.com/cudnn).
Can only be run on GPU.

Args:
    units: Positive integer, dimensionality of the output space.
    kernel_initializer: Initializer for the `kernel` weights matrix, used
      for the linear transformation of the inputs.
    unit_forget_bias: Boolean. 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)
    recurrent_initializer: Initializer for the `recurrent_kernel` weights
      matrix, used for the linear transformation of the recurrent state.
    bias_initializer: Initializer for the bias vector.
    kernel_regularizer: Regularizer function applied to the `kernel` weights
      matrix.
    recurrent_regularizer: Regularizer function applied to the
      `recurrent_kernel` weights matrix.
    bias_regularizer: Regularizer function applied to the bias vector.
    activity_regularizer: Regularizer function applied to the output of the
      layer (its "activation").
    kernel_constraint: Constraint function applied to the `kernel` weights
      matrix.
    recurrent_constraint: Constraint function applied to the
      `recurrent_kernel` weights matrix.
    bias_constraint: Constraint function applied to the bias vector.
    return_sequences: Boolean. Whether to return the last output. in the
      output sequence, or the full sequence.
    return_state: Boolean. Whether to return the last state in addition to
      the output.
    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.
c                   > Xl         [        R                  " SS5      nU" U R                   U R                   4S9U l        [        TU ]  " SUUUUS.UD6  [        R                  " U5      U l        [        R                  " U5      U l	        [        R                  " U5      U l
        XPl        [        R                  " U5      U l        [        R                  " U5      U l        [        R                  " U5      U l        [        R                  " U	5      U l        ["        R                  " U
5      U l        ["        R                  " U5      U l        ["        R                  " U5      U l        g )Ncell
state_size)r   )return_sequencesreturn_statego_backwardsstateful )unitscollections
namedtuple_cellsuper__init__r   getkernel_initializerrecurrent_initializerbias_initializerunit_forget_biasr   kernel_regularizerrecurrent_regularizerbias_regularizeractivity_regularizerr   kernel_constraintrecurrent_constraintbias_constraint)selfr   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r   r   r   r   kwargs	cell_spec	__class__s                      \/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/layers/rnn/cudnn_lstm.pyr   CuDNNLSTM.__init__J   s&   ( 
**6<@	4::tzz*BC
 	
-%%		

 	
 #/"2"23E"F%1%5%56K%L" , 0 01A B 0"."2"23E"F%1%5%56K%L" , 0 01A B$0$4$45I$J!!,1B!C$/OO4H$I!*?    c                     U R                   $ )N)r   )r'   s    r+   r   CuDNNLSTM.cellw   s    zzr-   c                 h  >^  [         TT ]  U5        [        U[        5      (       a  US   n[	        US   5      nT R                  UT R                  S-  4ST R                  T R                  T R                  S9T l
        T R                  T R                  T R                  S-  4ST R                  T R                  T R                  S9T l        T R                  (       a  U 4S jnOT R                   nT R                  T R                  S-  4S	UT R"                  T R$                  S9T l        S
T l        g )Nr      kernel)shapenameinitializerregularizer
constraintrecurrent_kernelc           	      2  > [         R                  " TR                  " TR                  S-  4/UQ70 UD6[         R                  R
                  R                  5       " TR                  4/UQ70 UD6TR                  " TR                  S-  4/UQ70 UD6/SS9$ )N      r   axis)tfconcatr   r   compatr	   ones_initializer)_argsr(   r'   s      r+   r   )CuDNNLSTM.build.<locals>.bias_initializer   s    yy--!ZZ!^-048> 		557!ZZM,04: --!ZZ!^-048>
  r-      biasT)r   build
isinstancelistint
add_weightr   r   r    r$   r3   r   r!   r%   r9   r   r   r"   r&   rG   built)r'   input_shape	input_dimr   r*   s   `   r+   rH   CuDNNLSTM.build{   s"   k"k4((%a.KB(	oodjj1n-////-- & 
 !%::tzzA~.#222200 !0 !
   "  $44OO::>#(--++ $ 
	 
r-   c                    U R                   (       d  [        R                  " USS9nUS   nUS   n[        R                  " USS9n[        R                  " USS9n[        R
                  " U R                  S S 2S U R                  24   U R                  S S 2U R                  U R                  S-  24   U R                  S S 2U R                  S-  U R                  S-  24   U R                  S S 2U R                  S-  S 24   U R                  S S 2S U R                  24   U R                  S S 2U R                  U R                  S-  24   U R                  S S 2U R                  S-  U R                  S-  24   U R                  S S 2U R                  S-  S 24   /U R                  S U R                   U R                  U R                  U R                  S-   U R                  U R                  S-  U R                  S-   U R                  U R                  S-  U R                  S-   U R                  U R                  S-  U R                  S	-   U R                  U R                  S	-  U R                  S
-   U R                  U R                  S
-  U R                  S-   U R                  U R                  S-  S  /U R                  S9nUUUUSS.n[        R                  R                  " S0 UD6u  pxn	  n
U R                  (       d  U R                  (       a
  US   nU	S   n	U R                  (       a*  U R                   (       a  UnO[        R                  " USS9nOUS   nXU	/4$ )N)   r   r<   )permr   rR   r=   r<      r2   r;         )weightsbiasesr4   T)inputinput_hinput_cparamsis_trainingr1   r   )
time_majorr?   	transposeexpand_dimsr   canonical_to_paramsr3   r   r9   rG   _vector_shaperaw_ops
CudnnRNNV2r   r   r   )r'   inputsinitial_staterZ   r[   r\   rD   outputshcrC   outputs               r+   _process_batchCuDNNLSTM._process_batch   s   \\&y9F""..q1..q133A||O,AtzzDJJN::;AtzzA~

Q>>?AtzzA~//0%%a4::o6%%adjj1n)D&DE%%aa$**q.)H&HI%%aa)9&9:	 		,DJJ'		$**tzzA~6		$**q.4::>:		$**q.4::>:		$**q.4::>:		$**q.4::>:		$**q.4::>:		$**q.*+	 $$+
2 
 !jj33;d;Aq!==D--!A!A   gI>R[F1v~r-   c                 6  > U R                   [        R                  " U R                  5      [        R                  " U R                  5      [        R                  " U R
                  5      U R                  [        R                  " U R                  5      [        R                  " U R                  5      [        R                  " U R                  5      [        R                  " U R                  5      [        R                  " U R                  5      [        R                  " U R                  5      [        R                  " U R                  5      S.n[         TU ]E  5       n[%        ['        UR)                  5       5      ['        UR)                  5       5      -   5      $ )N)r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   )r   r   	serializer   r   r   r   r   r    r!   r"   r#   r   r$   r%   r&   r   
get_configdictrJ   items)r'   configbase_configr*   s      r+   ro   CuDNNLSTM.get_config   s8   ZZ"."8"8''# &2%;%;**& !- 6 6t7L7L M $ 5 5"."8"8''# &2%;%;**& !- 6 6t7L7L M$0$:$:))% "-!6!6t7M7M!N$/$9$9))%  +44T5I5IJ1
4 g(*D**,-V\\^0DDEEr-   )r   r#   rG   r&   r   r"   rM   r3   r$   r   r    r%   r   r9   r!   r   r   )glorot_uniform
orthogonalzerosTNNNNNNNFFFF)__name__
__module____qualname____firstlineno____doc__r   propertyr   rH   rk   ro   __static_attributes____classcell__)r*   s   @r+   r   r       sk    &V ,* "!!#+@Z  2h4lF Fr-   r   )r|   r   tensorflow.compat.v2rA   v2r?   tf_keras.srcr   r   r   tf_keras.src.layers.rnnr   &tf_keras.src.layers.rnn.base_cudnn_rnnr    tensorflow.python.util.tf_exportr   r   r   r-   r+   <module>r      sS    '  ! ! $ % % 2 < : *+,`F	 `F -`Fr-   