o
    i e                     @   sJ   d Z ddlZddlm  mZ ddlmZ edgdG dd deZ	dS )zThe DetermisticRandomTestTool.

(from www.tensorflow.org/guide/migrate/validate_correctness) is a tool used to
make random number generation semantics match between TF1.x graphs/sessions and
eager execution.
    N)keras_exportz'keras.utils.DeterministicRandomTestTool)v1c                   @   sB   e Zd ZdZddefddZedd Zejd	d Zd
d Z	dS )DeterministicRandomTestToola  DeterministicRandomTestTool is a testing tool.

    This tool is used to validate random number generation semantics match
    between TF1.x graphs/sessions and eager execution.

    This is useful when you are migrating from TF 1.x to TF2 and need to make
    sure your computation is still happening correctly along the way. See the
    validating correctness migration guide for more info:
    https://www.tensorflow.org/guide/migrate/validate_correctness

    The following DeterministicRandomTestTool object provides a context manager
    scope() that can make stateful random operations use the same seed across
    both TF1 graphs/sessions and eager execution,The tool provides two testing
    modes:
    - constant which uses the same seed for every single operation no matter how
    many times it has been called and,
    - num_random_ops which uses the number of previously-observed stateful
    random operations as the operation seed.
    The num_random_ops mode serves as a more sensitive validation check than the
    constant mode. It ensures that the random numbers initialization does not
    get accidentaly reused.(for example if several weights take on the same
    initializations), you can use the num_random_ops mode to avoid this. In the
    num_random_ops mode, the generated random numbers will depend on the
    ordering of random ops in the program.

    This applies both to the stateful random operations used for creating and
    initializing variables, and to the stateful random operations used in
    computation (such as for dropout layers).

    Args:
      mode: Set mode to 'constant' or 'num_random_ops'. Defaults to
        'constant'.
      seed: The random seed to use.
    *   constantseedc                 C   sL   |dvrt dd|  tjtjjjj | _|| _	|| _
d| _t | _d S )N>   r   num_random_opsz1Mode arg must be 'constant' or 'num_random_ops'. zGot: r   )
ValueErrorsysmodulestfcompatr   get_seed
__module__seed_implementation_mode_seedoperation_seedset_observed_seeds)selfr   mode r   Y/var/www/myenv/lib/python3.10/site-packages/keras/src/legacy_tf_layers/migration_utils.py__init__5   s   z$DeterministicRandomTestTool.__init__c                 C   s   | j S N_operation_seedr   r   r   r   r   A   s   z*DeterministicRandomTestTool.operation_seedc                 C   s
   || _ d S r   r   )r   valuer   r   r   r   E   s   
c                    s6   t j j  fdd}t jjjjjj	 j
d|dS )zset random seed.c                    sx    j } jdkrtj| n(| jv r*tdd| d d d d d d	 d
  j|   j d7  _  j|fS )a  Wraps TF get_seed to make deterministic random generation easier.

            This makes a variable's initialization (and calls that involve
            random number generation) depend only on how many random number
            generations were used in the scope so far, rather than on how many
            unrelated operations the graph contains.

            Returns:
              Random seed tuple.
            r   zBThis `DeterministicRandomTestTool` object is trying to re-use the zalready-used operation seed z. z.It cannot guarantee random numbers will match z2between eager and sessions when an operation seed zis reused. You most likely set z2`operation_seed` explicitly but used a value that z1caused the naturally-incrementing operation seed z/sequences to overlap with an already-used seed.   )	r   r   r   randomset_seedr   r	   addr   )_op_seedr   r   r   	_get_seedN   s0   



z4DeterministicRandomTestTool.scope.<locals>._get_seedr   )wraps)r   r!   r"   r   r   r   testmockpatchobjectr   )r   r&   r   r   r   scopeI   s
   $z!DeterministicRandomTestTool.scopeN)r   r   )
__name__r   __qualname____doc__intr   propertyr   setterr,   r   r   r   r   r      s    #

r   )
r/   r
   tensorflow.compat.v2r   v2r    tensorflow.python.util.tf_exportr   r+   r   r   r   r   r   <module>   s    
