
    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Gated Recurrent Unit 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.GRUCell)v1c                      ^  \ rS rSrSr               S	U 4S jjr\R                  U 4S j5       rS
S jr	U 4S jr
SS jrSrU =r$ )GRUCell-   a+  Cell class for the GRU 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.GRU` processes the whole sequence.

For example:

>>> inputs = tf.random.normal([32, 10, 8])
>>> rnn = tf.keras.layers.RNN(tf.keras.layers.GRUCell(4))
>>> output = rnn(inputs)
>>> print(output.shape)
(32, 4)
>>> rnn = tf.keras.layers.RNN(
...    tf.keras.layers.GRUCell(4),
...    return_sequences=True,
...    return_state=True)
>>> whole_sequence_output, final_state = rnn(inputs)
>>> print(whole_sequence_output.shape)
(32, 10, 4)
>>> print(final_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`.
  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.
  reset_after: GRU convention (whether to apply reset gate after or
    before matrix multiplication). False = "before",
    True = "after" (default and cuDNN compatible).

Call arguments:
  inputs: A 2D tensor, with shape of `[batch, feature]`.
  states: A 2D tensor with shape of `[batch, units]`, which is the state
    from the 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        [&        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        [7        S[9        SU5      5      U l        [7        S[9        SU5      5      U l        UR                  S	S
5      nU R<                  S:w  a(  US:w  a"  [>        R@                  " [B        5        SU l"        OUU l"        UU l#        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_initializerr   kernel_regularizerrecurrent_regularizerbias_regularizerr   kernel_constraintrecurrent_constraintbias_constraintminmaxdropoutrecurrent_dropoutloggingdebugRECURRENT_DROPOUT_WARNING_MSGr   reset_after
state_sizeoutput_size)selfr&   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r6   r7   r;   kwargsr   	__class__s                      U/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/layers/rnn/gru.pyr%   GRUCell.__init__w   s   ( A:4497!= 
 99<<;;==*0**'+D' +1**'+D' 	"6"
%//*5$/OO4H$I! "."2"23E"F%1%5%56K%L" , 0 01A B"."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&**::    c           	        > [         TU ]  U5        US   n[        R                  " U 5      nU R	                  X R
                  S-  4SU R                  U R                  U R                  US9U l	        U R	                  U R
                  U R
                  S-  4SU R                  U R                  U R                  US9U l        U R                  (       ak  U R                  (       d  SU R
                  -  4nOSSU R
                  -  4nU R	                  USU R                   U R"                  U R$                  US9U l        OS U l        SU l        g )	N   kernel)shapenameinitializerregularizer
constraintcaching_devicerecurrent_kernelr   biasT)r$   buildr   rM   
add_weightr&   r+   r.   r1   rG   r,   r/   r2   rN   r*   r;   r-   r0   r3   rO   built)r>   input_shape	input_dimdefault_caching_device
bias_shaper@   s        rA   rP   GRUCell.build   s9   k"O	!*!9!9$!?oojj1n-////--1 & 
 !%::tzzA~.#2222001 !0 !
 ==##$**n.
  TZZ0
  11 11//5 ( DI DI
rC   c                    [         R                  R                  U5      (       a  US   OUnU R                  XSS9nU R	                  XCSS9nU R
                  (       aA  U R                  (       d  U R                  S pO"[         R                  " U R                  5      u  pxU R                  S:X  Ga  SU R                  s=:  a  S:  a  O  OXS   -  n	XS   -  n
XS   -  nOUn	Un
Un[        R                  " XR                  S S 2S U R                  24   5      n[        R                  " XR                  S S 2U R                  U R                  S-  24   5      n[        R                  " XR                  S S 2U R                  S-  S 24   5      nU R
                  (       az  [        R                  " UWS U R                   5      n[        R                  " XU R                  U R                  S-   5      n[        R                  " XU R                  S-  S  5      nSU R                   s=:  a  S:  a  O  OXFS   -  nXFS   -  nXFS   -  nOUnUnUn[        R                  " XR"                  S S 2S U R                  24   5      n[        R                  " UU R"                  S S 2U R                  U R                  S-  24   5      nU R                  (       ae  U R
                  (       aT  [        R                  " UWS U R                   5      n[        R                  " UXR                  U R                  S-   5      nU R%                  UU-   5      nU R%                  UU-   5      nU R                  (       au  [        R                  " UU R"                  S S 2U R                  S-  S 24   5      nU R
                  (       a'  [        R                  " UWU R                  S-  S  5      nUU-  nO:[        R                  " UU-  U R"                  S S 2U R                  S-  S 24   5      nU R'                  UU-   5      nGOSU R                  s=:  a  S:  a
  O  OXS   -  n[        R                  " XR                  5      nU R
                  (       a  [        R                  " UW5      n[         R(                  " USSS	9u  pnU R                  (       aI  [        R                  " X@R"                  5      nU R
                  (       a  [        R                  " UW5      nO6[        R                  " X@R"                  S S 2S SU R                  -  24   5      n[         R(                  " UU R                  U R                  S/SS	9u  nnnU R%                  UU-   5      nU R%                  UU-   5      nU R                  (       a  UU-  nO:[        R                  " UU-  U R"                  S S 2SU R                  -  S 24   5      nU R'                  UU-   5      nUU-  SU-
  U-  -   n[         R                  R                  U5      (       a  U/OUnUU4$ )
Nr   rF   countr   r   r   r   rE   axis)r   nest	is_nestedget_dropout_mask_for_cell#get_recurrent_dropout_mask_for_cellr*   r;   rO   unstackr   r6   r   dotrG   r&   bias_addr7   rN   r)   r(   split)r>   inputsstatestrainingh_tm1dp_maskrec_dp_mask
input_biasrecurrent_biasinputs_zinputs_rinputs_hx_zx_rx_hh_tm1_zh_tm1_rh_tm1_hrecurrent_zrecurrent_rzrrecurrent_hhhmatrix_xmatrix_innerh	new_states                               rA   callGRUCell.call   s]   **622F1I 	 000K>>1 ? 
 ==##-1YYN-/ZZ		-B*
!#T\\'C'!AJ.!AJ.!AJ.!!!++hA||O(DEC++++adjj1n)D&DEC ++hAtzzA~7G4G(HIC}}&&sJ|,DE&&DJJa@ &&stzzA~7G,HIT++1c1a.0a.0a.0!++..q,DJJ,?K "++..q$**tzzA~2M/MNK DMM%..$**!= &..

TZZ!^!L ))#*;<A))#*;<A %kkT221djj1n6F3FG ==")"2"2#^DJJN4D%E#K  +o%kkK!6!6q$**q.:J7J!K {!23BT\\'C'!*, {{6;;7H}}"++HjAHHXqr:MCc&{{52G2GH==#*#3#3$n$L
  '{{004Da$**n4D1DE  57HHtzz4::r:51Kk ))#*;<A))#*;<A+o%kkIt44QDJJ8H5HI {!23BIQ"$77,,V44QC!	)|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[        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(                  _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   r6   r7   r   r;   )r&   r   	serializer(   r)   r*   r   r+   r,   r-   r   r.   r/   r0   r   r1   r2   r3   r6   r7   r   r;   updater    config_for_enable_caching_devicer$   
get_configdictlistitemsr>   configbase_configr@   s      rA   r   GRUCell.get_configa  s
   
TZZ
+//@
 #K$9$9))%
 
 !,"8"8''#
 $\%;%;**&
  6 6t7L7L M
 !,"8"8''#
" $\%;%;**&#
(  6 6t7L7L M)
*  !6!6t7M7M!N+
, #K$9$9))%-
2 {44T5I5IJ3
4 t||5
6  !7!77
8 d119
: 4++;
> 	i@@FGg(*D**,-V\\^0DDEErC   c                 0    [         R                  " XX#5      $ N)r   #generate_zero_filled_state_for_cell)r>   re   
batch_sizedtypes       rA   get_initial_stateGRUCell.get_initial_state  s    <<*
 	
rC   )r#   r(   rO   r3   r-   r0   rR   r6   r   rG   r1   r+   r.   r=   r)   r2   r7   r,   rN   r/   r;   r<   r&   r*   )tanhsigmoidTglorot_uniform
orthogonalzerosNNNNNNr   r   Tr   NNN)__name__
__module____qualname____firstlineno____doc__r%   r   shape_type_conversionrP   r   r   r   __static_attributes____classcell__r@   s   @rA   r   r   -   sr    FV &+* "!#?&B ##( $(T||"FH
 
rC   r   zkeras.layers.GRUc                     ^  \ 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SrU =r$ )GRUi  ax  Gated Recurrent Unit - Cho et al. 2014.

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. `reset_after` is `True`
7. Inputs, if use masking, are strictly right-padded.
8. Eager execution is enabled in the outermost context.

There are two variants of the GRU implementation. The default one is based
on [v3](https://arxiv.org/abs/1406.1078v3) and has reset gate applied to
hidden state before matrix multiplication. The other one is based on
[original](https://arxiv.org/abs/1406.1078v1) and has the order reversed.

The second variant is compatible with CuDNNGRU (GPU-only) and allows
inference on CPU. Thus it has separate biases for `kernel` and
`recurrent_kernel`. To use this variant, set `reset_after=True` and
`recurrent_activation='sigmoid'`.

For example:

>>> inputs = tf.random.normal([32, 10, 8])
>>> gru = tf.keras.layers.GRU(4)
>>> output = gru(inputs)
>>> print(output.shape)
(32, 4)
>>> gru = tf.keras.layers.GRU(4, return_sequences=True, return_state=True)
>>> whole_sequence_output, final_state = gru(inputs)
>>> print(whole_sequence_output.shape)
(32, 10, 4)
>>> print(final_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`.
  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.
  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.
  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.
  reset_after: GRU convention (whether to apply reset gate after or
    before matrix multiplication). False = "before",
    True = "after" (default and cuDNN compatible).

Call arguments:
  inputs: A 3D tensor, with shape `[batch, timesteps, feature]`.
  mask: Binary tensor of shape `[samples, 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           
      6  > 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                  [        R                  [        R                  4;   =(       a    U R                   [        R"                  [        R"                  4;   =(       aT    US:H  =(       aH    U(       + =(       a:    U=(       a1    U=(       a(    [        R$                  R&                  R)                  5       U l        [        R,                  R/                  S5      (       at  U R*                  (       a2  [        R0                  " [2        R4                  U R6                  -  5        O1[        R                  " [2        R8                  U R6                  -  5        [2        R:                  " 5       (       a  [2        R<                  " UUS 5      U l        g g )!Nreturn_runtimeFr   r   r   zm`implementation=0` has been deprecated, and now defaults to `implementation=2`.Please update your layer call.r   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r6   r7   r;   r   	trainableTrI   gru_cell)return_sequencesreturn_statego_backwardsstatefulunroll
time_majorrF   )ndimGPUgru) r"   _return_runtimer8   warningr   r'   r$   r%   r   activity_regularizerr	   
input_specr(   r   r   r   r)   r   r    r   r!   _could_use_gpu_kernelr   list_logical_devicesr9   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   r   r1   r2   r3   r6   r7   r   r   r   r   r   r   r;   r?   r   cell_kwargscellr@   s                               rA   r%   GRU.__init__  s   :  &zz*:EB$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!$!,- OO 0 0"'':: C))k.A.A2::-NNC!Q&C 
C 	C
 C 		@@B 	" 99))%00 ))n@@499LM"::TYYF //11"0"="=L%#D 2rC   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OT R'                  XX2U5      u  ppT R(                  (       ax  [*        R,                  R.                  R1                  T R2                  S   [*        R4                  " US   T R2                  S   R6                  5      5      /nT R9                  U5        T R                  (       a"  [         R:                  " UUUT R                  S9nOUnT R<                  (       a  U/[        U5      -   $ T R>                  (       a  UU4$ U$ )Nr   r   rg   c                 *   > TR                   " X40 TD6$ r   )r   )cell_inputscell_statesr?   r>   s     rA   stepGRU.call.<locals>.step  s    yyDVDDrC   )	constantsr   maskr   input_lengthr   zero_output_for_maskreturn_all_outputs)r   ) r   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_defun_gru_callr   r   r    r   assignrf   castr   
add_updatemaybe_convert_to_raggedr   r   )r>   re   r   rg   initial_staterow_lengthsis_ragged_input_rS   	timestepsr   last_outputoutputsrf   r   updatesoutputr?   s   `                @rA   r   GRU.callu  s    &>>vF%T1%%o< $(#7#74$
 q dD!!7D''/&*ooKN;q>	)) (+F//		:E ,3;;!..{{* )??%)%>%>#'#8#8,(K&  %,,^-K-KLG484H4Hx{51K' ==		##KKNBGGF1It{{1~7K7K$LG
 OOG$  44!..	F !F8d6l**!!7?"MrC   c                 .    U R                   R                  $ r   )r   r&   r>   s    rA   r&   	GRU.units  s    yyrC   c                 .    U R                   R                  $ r   )r   r(   r   s    rA   r(   GRU.activation  s    yy###rC   c                 .    U R                   R                  $ r   )r   r)   r   s    rA   r)   GRU.recurrent_activation      yy---rC   c                 .    U R                   R                  $ r   )r   r*   r   s    rA   r*   GRU.use_bias  s    yy!!!rC   c                 .    U R                   R                  $ r   )r   r+   r   s    rA   r+   GRU.kernel_initializer      yy+++rC   c                 .    U R                   R                  $ r   )r   r,   r   s    rA   r,   GRU.recurrent_initializer      yy...rC   c                 .    U R                   R                  $ r   )r   r-   r   s    rA   r-   GRU.bias_initializer      yy)))rC   c                 .    U R                   R                  $ r   )r   r.   r   s    rA   r.   GRU.kernel_regularizer  r   rC   c                 .    U R                   R                  $ r   )r   r/   r   s    rA   r/   GRU.recurrent_regularizer  r   rC   c                 .    U R                   R                  $ r   )r   r0   r   s    rA   r0   GRU.bias_regularizer  r   rC   c                 .    U R                   R                  $ r   )r   r1   r   s    rA   r1   GRU.kernel_constraint      yy***rC   c                 .    U R                   R                  $ r   )r   r2   r   s    rA   r2   GRU.recurrent_constraint  r   rC   c                 .    U R                   R                  $ r   )r   r3   r   s    rA   r3   GRU.bias_constraint  s    yy(((rC   c                 .    U R                   R                  $ r   )r   r6   r   s    rA   r6   GRU.dropout  s    yy   rC   c                 .    U R                   R                  $ r   )r   r7   r   s    rA   r7   GRU.recurrent_dropout  r   rC   c                 .    U R                   R                  $ r   )r   r   r   s    rA   r   GRU.implementation  s    yy'''rC   c                 .    U R                   R                  $ r   )r   r;   r   s    rA   r;   GRU.reset_after  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[        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*                  _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   r   r1   r2   r3   r6   r7   r   r;   r   ) r&   r   r   r(   r)   r*   r   r+   r,   r-   r   r.   r/   r0   r   r   r1   r2   r3   r6   r7   r   r;   r   r   r   r   r$   r   r   r   r   r   s      rA   r   GRU.get_config  s9   !
TZZ!
+//@!
 #K$9$9))%!
 !
 !,"8"8''#!
 $\%;%;**&!
  6 6t7L7L M!
 !,"8"8''#!
" $\%;%;**&#!
(  6 6t7L7L M)!
* #L$:$:))%+!
0  !6!6t7M7M!N1!
2 #K$9$9))%3!
8 {44T5I5IJ9!
: t||;!
<  !7!7=!
> d11?!
@ 4++A!
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GRU.from_config)  s.    v%&1A*Ba*G'(F#$}V}rC   c                    U R                  5         U R                  XSS9nUb  XS   -  n[        R                  " 5       (       a  U[        R                  " US   5      [        R                  " U R
                  R                  5      [        R                  " U R
                  R                  5      [        R                  " U R
                  R                  5      UU R                  U R                  UU R                  S.
nU R                  R                  " S0 UD6u  nn	n
nGOU[        R                  " US   5      [        R                  " U R
                  R                  5      [        R                  " U R
                  R                  5      [        R                  " U R
                  R                  5      UU R                  U R                  UU R                  S.
nUR                  5       nUR!                  SU R                  05        ["        R$                  " 5       (       a  [        R&                  " 5       nU[        R(                  :H  =(       d*    US L =(       a    ["        R*                  R-                  S5      =(       a!    [        R.                  " X@R                  U5      nU(       a  [1        S0 UD6u  ppO[3        S0 UD6u  ppO[5        S0 UD6u  nn	n
nU
/nXUU4$ )	NrF   rY   r   )
re   init_hrG   rN   rO   r   r   r   sequence_lengthsr   
re   r  rG   rN   rO   r   r   r   r  r   r   r   r   )reset_dropout_maskr_   r
   r   read_variable_valuer   rG   rN   rO   r   r   r   r   defun_layerr   copyr   r   executing_eagerlyget_context_device_typeGPU_DEVICE_NAMEr   r   is_cudnn_supported_inputsgpu_grustandard_grugru_with_backend_selection)r>   re   r   rg   r   r  dropout_mask
gru_kwargsr   r   new_hr   gpu_gru_kwargsnormal_gru_kwargsdevice_typecan_use_gpurf   s                    rA   r   GRU._defun_gru_call/  s    	!55fa5P#1o-F//11 (<<]1=MN(<<TYY=M=MN$2$F$FII..% '::499>>J"oo $ 1 1$4(,(A(AJ& ##//=*= !(<<]1=MN(<<TYY=M=MN$2$F$FII..% '::499>>J"oo $ 1 1$4$($9$9N !/ 3 3 5$$*D,E,E ##%%,DDF
 $~'E'EE '4/ F "		 > >u E	 '@@ //3C " ;B <(<8K% <H <+<8K% /C1BC Wf44rC   )r   r   r   r   r   )r   r   Tr   r   r   NNNNNNNr   r   FFFFFFTr   )r   r   r   r   r   r%   r   propertyr&   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r6   r7   r   r;   r   classmethodr  r   r   r   r   s   @rA   r   r     s   ~F &+* "!!1fPFP   $ $ . . " " , , / / * * , , / / * * + + . . ) ) ! ! + + ( ( % %&FP  
\5 \5rC   r   c                 @  ^^^^ [         R                  " U 5      nU(       a  US   OUS   n[        R                  " U5      u  mmUUUU4S jn[         R                  " UU U/SSUUUUb  UOUU	U
S9u  pnUUUS   [
        R                  " [
        R                  5      4$ )aq  GRU with standard kernel implementation.

This implementation can be run on all types of 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.

Args:
  inputs: Input tensor of GRU layer.
  init_h: Initial state tensor for the cell output.
  kernel: Weights for cell kernel.
  recurrent_kernel: Weights for cell recurrent kernel.
  bias: Weights for cell kernel bias and recurrent bias. The bias contains
    the combined input_bias and recurrent_bias.
  mask: Binary tensor of shape `(samples, timesteps)` indicating whether
    a given timestep should be masked. 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.
  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[         R                  " U T5      n[         R                  " UT5      n[        R                  " USSS9u  pEn[         R                  " UT5      n[         R                  " UT5      n[        R                  " USSS9u  pn
[        R
                  " XH-   5      n[        R
                  " XY-   5      n[        R                  " XlU
-  -   5      nX-  SU-
  U-  -   nX/4$ )z8Step function that will be used by TF-Keras RNN backend.r   rF   r   r[   )r   rb   rc   r   rd   r   r   )r   r   rh   r|   rp   rq   rr   r}   rv   rw   rz   rx   ry   r{   r~   rk   rG   rl   rN   s                  rA   r   standard_gru.<locals>.step  s    A ;;{F3##Hj9115# {{5*:;''nE02!!1
-+ JJs()JJs()WWS{?*+ IQ"$#vrC   NF)r   r   r   r   r   r   r   r   )r   r   r   ra   r   r
   r   RUNTIME_CPU)re   r  rG   rN   rO   r   r   r   r  r   r   rS   r   r   r   r   
new_statesrk   rl   s     ``             @@rA   r  r    s    p ##F+K",A+a.I!#D!1J 2 (/{{	!' &1+($K*  	1~99:	 rC   c
                 F   Ub  [         R                  " XV5      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
                  " USSS9nU[        R
                  " USSS9-  n[        R
                  " [        R                  " U5      S5      n[        R                  R                  5       S	   (       aD  US   US
   sUS
'   US'   US   US   sUS'   US'   US   US
   sUS
'   US'   US   US   sUS'   US'   [         R                  " UU[        R                  " S/5      SS9nUb{  U(       a  [        R                  " U UU
US9n [        R                  R                  U US
USSUUS9u  p    nU(       a,  [        R                  " UUU
US9n[        R                  " X/S9nOCU(       a  [        R                  " U S
/S9n [        R                  R!                  U US
USSS9u  p  nUS   nU(       d!  Uc  U	(       a  [        R                  " U/ SQS9n[        R"                  " XS9nUb  UnU	(       d  [        R                  " UU(       a  S
OSS9nUUU[         R$                  " [         R&                  5      4$ )z>GRU with cuDNN implementation which is only available for GPU.)r   r   r   )perm)r   r   )r   r   r[   rF   r      is_cuda_buildr      rE   T)weightsbiasesrH   transpose_weights)seq_axis
batch_axisr   )inputinput_hinput_cparamsis_trainingrnn_moder  r   )r:  r;  r<  r=  r>  r?  )r
   calculate_sequence_by_maskr   	transposeexpand_dimsrd   r   flatten	sysconfigget_build_infocanonical_to_paramsconstantreverse_sequenceraw_ops
CudnnRNNV3reverseCudnnRNNsqueezer   RUNTIME_GPU)re   r  rG   rN   rO   r   r   r   r  r   r8  r9  r5  r=  r   r~   r   r   s                     rA   r  r    s    )DD
 *2f95%*)3v ^^F2Fhhvqq)Grxx(!!44G 88GOOD)1-D	||""$_5 ")WQZ
GAJ!(WQZ
GAJ7DGQa7DGQa//kk2$	F # (( !%	F !jj33-! 4 	
Aq! )) !%	G jjz:GZZaS1F::.. / 
Aq "+K*27G,,wY7


1$A # ..
1J 		~99:	 rC   c                 >  ^^ U UUUUUUUUU	U
S.mS m[         R                  " 5       (       aS  [        R                  R	                  [         R
                  U4S j[         R                  UU4S j0U4S j5      u  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  pp[         R                  " U40 TD6  XX4$ )	a   Call the GRU 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 GRU layer.
  init_h: Initial state tensor for the cell output.
  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_gru.
re   r  rG   rN   rO   r   r   r   r  r   r   c                    ^ ^^^^^^^^^	^
 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	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S9
$ )Nr  )r  )
rO   r   r  re   rG   r   rN   r   r  r   s
   rA   cudnn_gru_fnOgru_with_backend_selection.<locals>.gpu_gru_with_fallback.<locals>.cudnn_gru_fn  s.    !1%)!1!1 rC   c                  *   > [        TTTTT TT	TTT
TS9$ )NrP  r  )rO   r   r  re   rG   r   rN   r   r  r   r   s   rA   standard_gru_fnRgru_with_backend_selection.<locals>.gpu_gru_with_fallback.<locals>.standard_gru_fn  s1    !1%)!1%9!1 rC   )true_fnfalse_fn)r   __internal__
smart_condr
   r  )re   r  rG   rN   rO   r   r   r   r  r   r   rS  rW  s   ```````````  rA   gpu_gru_with_fallback9gru_with_backend_selection.<locals>.gpu_gru_with_fallback  sZ    	 		 	 ))4444j"2 !$ 5 
 	
rC   c                     > [        S0 T D6$ Nr   rV  r=  s   rA   <lambda>,gru_with_backend_selection.<locals>.<lambda>  s    8Nv8NrC   c                     > T " S0 TD6$ r`  r   )r]  r=  s   rA   rb  rc    s    8M 99rC   c                     > [        S0 T D6$ r`  rV  ra  s   rA   rb  rc    s    L*6*rC   gru_)r   r   r   )r
   r   r   r[  execute_fn_for_deviceCPU_DEVICE_NAMEr  struuiduuid4generate_defun_backendr  function_register)re   r  rG   rN   rO   r   r   r   r  r   r   r   r   r#  r   api_namesupportive_attributedefun_standard_grudefun_gpu_grur]  r=  s                      @@rA   r   r   v  s/   f , $, 4,F2
h ++-- OO11..0N.. 1 +
	
 C

--$( 
 ,BB** 	
 '==**! 	
 0B/KF/K,e((A&A//rC   )%r   rj  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   r8    tensorflow.python.util.tf_exportr   r:   BaseRandomLayerr   r   r  r  r   r   rC   rA   <module>r~     s    "  ! ! $   $ % % * 4 2 - 0 N ' = 9   $,Z
!:#=#= Z
 -Z
z
  R(5
sJ$>$> 5 )5DjZxv`0rC   