
    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 GRU layer backed by cuDNN.    N)constraints)initializers)regularizers)gru_lstm_utils)	_CuDNNRNN)keras_exportzkeras.layers.CuDNNGRU)v1c                   z   ^  \ 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$ )
CuDNNGRU    a  Fast GRU 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.
    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                   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
        [        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_initializerr   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   kwargs	cell_spec	__class__s                     [/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/layers/rnn/cudnn_gru.pyr   CuDNNGRU.__init__F   s   & 
**6<@	$**5
 	
-%%		

 	
 #/"2"23E"F%1%5%56K%L" , 0 01A B"."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   CuDNNGRU.cellq   s    zzr,   c                 0  > [         TU ]  U5        [        U[        5      (       a  US   n[	        US   5      nU R                  X R                  S-  4SU R                  U R                  U R                  S9U l
        U R                  U R                  U R                  S-  4SU R                  U R                  U R                  S9U l        U R                  U R                  S-  4SU R                  U R                   U R"                  S9U l        S	U l        g )
Nr      kernel)shapenameinitializerregularizer
constraintrecurrent_kernel   biasT)r   build
isinstancelistint
add_weightr   r   r   r#   r2   r   r    r$   r8   r   r!   r%   r:   built)r&   input_shape	input_dimr)   s      r*   r;   CuDNNGRU.buildu   s
   k"k4((%a.KB(	oojj1n-////-- & 
 !%::tzzA~.#222200 !0 !
 OO::>#----++ $ 
	 
r,   c                 <   U R                   (       d  [        R                  " USS9nUS   n[        R                  " USS9n[        R
                  " U R                  S S 2U R                  U R                  S-  24   U R                  S S 2S U R                  24   U R                  S S 2U R                  S-  S 24   U R                  S S 2U R                  U R                  S-  24   U R                  S S 2S U R                  24   U R                  S S 2U R                  S-  S 24   /U R                  U R                  U R                  S-   U R                  S U R                   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SUS
SS.n[        R                  R                  " S0 UD6u  pg    nU R                  (       d  U R                  (       a  US   nU R                  (       a*  U R                   (       a  Un	O[        R                  " USS9n	OUS   n	X/4$ )N)   r      )permr   )axisrF   r1         )weightsbiasesr3   Tgru)inputinput_hinput_cparamsis_trainingrnn_moder0   r   )
time_majortf	transposeexpand_dimsr   canonical_to_paramsr2   r   r8   r:   _vector_shaperaw_ops
CudnnRNNV2r   r   r   )
r&   inputsinitial_staterO   rQ   argsoutputsh_outputs
             r*   _process_batchCuDNNGRU._process_batch   s6   \\&y9F"..q133AtzzDJJN::;A||O,AtzzA~//0%%adjj1n)D&DE%%a4::o6%%aa)9&9: 		$**tzzA~6		,DJJ'		$**q.4::>:		$**q.4::>:		$**q.4::>:		$**q.*+ $$#
* 
 !jj33;d;Aq!==D--!A   gI>R[Fs{r,   c                    > 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      [        R                  " U R                  5      [        R                  " U R                  5      [        R                  " U R                  5      S.n[        TU ]A  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   	serializer   r   r   r   r   r    r!   r"   r   r#   r$   r%   r   
get_configdictr=   items)r&   configbase_configr)   s      r*   rg   CuDNNGRU.get_config   s/   ZZ"."8"8''# &2%;%;**& !- 6 6t7L7L M"."8"8''# &2%;%;**& !- 6 6t7L7L M$0$:$:))% "-!6!6t7M7M!N$/$9$9))%  +44T5I5IJ/
2 g(*D**,-V\\^0DDEEr,   )r   r"   r:   r%   r   r!   r@   r2   r#   r   r   r$   r   r8   r    r   )glorot_uniform
orthogonalzerosNNNNNNNFFFF)__name__
__module____qualname____firstlineno____doc__r   propertyr   r;   rc   rg   __static_attributes____classcell__)r)   s   @r*   r   r       sh    "N ,* "!!!)@V  @.`F Fr,   r   )rt   r   tensorflow.compat.v2compatv2rU   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y F ,Fr,   