a
    Sic                     @   sb   d Z ddlm  mZ ddlmZ ddlmZ	 dd Z
dd Zd	d
 Zdd Zdd Zdd ZdS )z#Utilities for RNN cells and layers.    N)control_flow_util)
tf_loggingc                 C   s   t | tr|du r|du sJ |r>| | d }| d|  } t| dkrb| dd }| dd } t| dkrxt| } n| d } dd }||}||}| ||fS )a  Standardizes `__call__` to a single list of tensor inputs.

    When running a model loaded from a file, the input tensors
    `initial_state` and `constants` can be passed to `RNN.__call__()` as part
    of `inputs` instead of by the dedicated keyword arguments. This method
    makes sure the arguments are separated and that `initial_state` and
    `constants` are lists of tensors (or None).

    Args:
      inputs: Tensor or list/tuple of tensors. which may include constants
        and initial states. In that case `num_constant` must be specified.
      initial_state: Tensor or list of tensors or None, initial states.
      constants: Tensor or list of tensors or None, constant tensors.
      num_constants: Expected number of constants (if constants are passed as
        part of the `inputs` list.

    Returns:
      inputs: Single tensor or tuple of tensors.
      initial_state: List of tensors or None.
      constants: List of tensors or None.
    N   r   c                 S   s.   | d u st | tr| S t | tr(t| S | gS N)
isinstancelisttuple)x r
   V/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/layers/rnn/rnn_utils.pyto_list_or_noneL   s
    
z)standardize_args.<locals>.to_list_or_none)r   r   lenr   )inputsinitial_state	constantsnum_constantsr   r
   r
   r   standardize_args   s    

r   c                 C   s   t | dot| tj S )z6Check whether the state_size contains multiple states.__len__)hasattrr   tfTensorShape)
state_sizer
   r
   r   is_multiple_stateY   s    r   c                 C   s*   |d urt |d }|j}t|| j|S )Nr   )r   shapedtypegenerate_zero_filled_stater   )cellr   
batch_sizer   r
   r
   r   #generate_zero_filled_state_for_cell`   s    r   c                    sX    du sdu r$t d  d  fdd}tj|rLtj||S ||S dS )zBGenerate a zero filled tensor with shape [batch_size, state_size].Nz[batch_size and dtype cannot be None while constructing initial state. Received: batch_size=z, dtype=c                    s&   t |  } g| }t j|dS )N)r   )r   r   as_listzeros)unnested_state_size	flat_dimsinit_state_sizebatch_size_tensorr   r
   r   create_zeroso   s    
z0generate_zero_filled_state.<locals>.create_zeros)
ValueErrorr   nest	is_nestedmap_structure)r%   r   r   r&   r
   r$   r   r   g   s    r   c                 C   sb   t  rdS t| ddsdS tt jj r<t	d dS | j
j| j
jkrZt	d dS dd S )a  Returns the caching device for the RNN variable.

    This is useful for distributed training, when variable is not located as
    same device as the training worker. By enabling the device cache, this
    allows worker to read the variable once and cache locally, rather than read
    it every time step from remote when it is needed.

    Note that this is assuming the variable that cell needs for each time step
    is having the same value in the forward path, and only gets updated in the
    backprop. It is true for all the default cells (SimpleRNN, GRU, LSTM). If
    the cell body relies on any variable that gets updated every time step, then
    caching device will cause it to read the stall value.

    Args:
      rnn_cell: the rnn cell instance.
    N_enable_caching_deviceFa$  Variable read device caching has been disabled because the RNN is in tf.while_loop loop context, which will cause reading stalled value in forward path. This could slow down the training due to duplicated variable reads. Please consider updating your code to remove tf.while_loop if possible.a\  Variable read device caching has been disabled since it doesn't work with the mixed precision API. This is likely to cause a slowdown for RNN training due to duplicated read of variable for each timestep, which will be significant in a multi remote worker setting. Please consider disabling mixed precision API if the performance has been affected.c                 S   s   | j S r   )device)opr
   r
   r   <lambda>       z caching_device.<locals>.<lambda>)r   executing_eagerlygetattrr   IsInWhileLoopcompatv1get_default_graphloggingwarning_dtype_policycompute_dtypevariable_dtype)rnn_cellr
   r
   r   caching_devicez   s$    	r<   c                 C   s$   t jj }| j|kr d| jiS i S )aw  Return the dict config for RNN cell wrt to enable_caching_device field.

    Since enable_caching_device is a internal implementation detail for speed up
    the RNN variable read when running on the multi remote worker setting, we
    don't want this config to be serialized constantly in the JSON. We will only
    serialize this field when a none default value is used to create the cell.
    Args:
      rnn_cell: the RNN cell for serialize.

    Returns:
      A dict which contains the JSON config for enable_caching_device value or
      empty dict if the enable_caching_device value is same as the default
      value.
    enable_caching_device)r   r3   r4   #executing_eagerly_outside_functionsr+   )r;   default_enable_caching_devicer
   r
   r    config_for_enable_caching_device   s
    


r@   )__doc__tensorflow.compat.v2r3   v2r   keras.utilsr   tensorflow.python.platformr   r6   r   r   r   r   r<   r@   r
   r
   r
   r   <module>   s   ?5