
    &Vf)                         d dl Zd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ  ed           G d d	                      Zd
 Zd Zd ZdS )    N)backend)keras_export)global_state)	jax_utils)	auto_namezkeras.random.SeedGeneratorc                   >    e Zd ZdZddZd	dZd Zed             ZdS )
SeedGeneratoraA  Generates variable seeds upon each call to a RNG-using function.

    In Keras, all RNG-using methods (such as `keras.random.normal()`)
    are stateless, meaning that if you pass an integer seed to them
    (such as `seed=42`), they will return the same values at each call.
    In order to get different values at each call, you must use a
    `SeedGenerator` instead as the seed argument. The `SeedGenerator`
    object is stateful.

    Example:

    ```python
    seed_gen = keras.random.SeedGenerator(seed=42)
    values = keras.random.normal(shape=(2, 3), seed=seed_gen)
    new_values = keras.random.normal(shape=(2, 3), seed=seed_gen)
    ```

    Usage in a layer:

    ```python
    class Dropout(keras.Layer):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.seed_generator = keras.random.SeedGenerator(1337)

        def call(self, x, training=False):
            if training:
                return keras.random.dropout(
                    x, rate=0.5, seed=self.seed_generator
                )
            return x
    ```
    Nc                     |t           j        j                  }| _        |                    dd           }|rt          d|           || _        nt           _         _        t                      t          t                    st          d            fd}t          j         j                   5   j                            |dddd	
           _        d d d            d S # 1 swxY w Y   d S )Nr   z Unrecognized keyword arguments: z3Argument `seed` must be an integer. Received: seed=c                  l    |                     dd           }j                            dg|          S )Ndtyper   r   )getr   convert_to_tensor)argskwargsr   seedselfs      \/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/random/seed_generator.pyseed_initializerz0SeedGenerator.__init__.<locals>.seed_initializerF   s5    JJw--E<114)51III    )caller)   uint32Fseed_generator_state)shaper   	trainablename)r   	__class____name__r   pop
ValueErrorr   _initial_seedmake_default_seed
isinstanceint
name_scopeVariablestate)r   r   r   r   custom_backendr   s   ``    r   __init__zSeedGenerator.__init__0   s   <T^455D	It44 	JHHHIII%)DLL"DL!<$&&D$$$ 	OOO  	J 	J 	J 	J 	J 	J 	$777 	 	.. + /  DJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   %C66C:=C:Tc                    | j         }|j        dz  }|rN| j                            t	          j        ddg          d          }| j                             ||z              n#| j                             |dz   dz  dz             |S )N   r   r   r   i  iO= )r(   valuer   r   nparrayassign)r   ordered
seed_statenew_seed_value	increments        r   nextzSeedGenerator.nextS   s    Z
#)A- 	@66!Q   7  I Jj945555 JzA~5>???r   c                     d| j         iS )Nr   )r"   )r   s    r   
get_configzSeedGenerator.get_configb   s    *++r   c                      | di |S )N r9   )clsconfigs     r   from_configzSeedGenerator.from_confige   s    s}}V}}r   )NN)T)	r   
__module____qualname____doc__r*   r5   r7   classmethodr<   r9   r   r   r	   r	      sr           D! ! ! !F   , , ,   [  r   r	   c                      t          j                    rt          d          t          j        d          } | #t                      } t          j        d|            | S )Na  [JAX RNG] When tracing a JAX function, you should only use seeded random ops, e.g. you should create a `SeedGenerator` instance, attach it to your layer/model, and pass the instance as the `seed` argument when calling random ops. Unseeded random ops would get incorrectly traced by JAX and would become constant after tracing. Example:

```
# Make sure to set the seed generator as a layer attribute
self.seed_generator = keras.random.SeedGenerator(seed=1337)
...
out = keras.random.normal(shape=(1,), seed=self.seed_generator)
```global_seed_generator)r   is_in_jax_tracing_scoper!   r   get_global_attributer	   set_global_attribute)gens    r   rB   rB   j   sd    (** 

 
 	
 
+,C
D
DC
{oo)*A3GGGJr   c                  F    t          j        dt          d                    S )Nr,   g    eA)python_randomrandintr%   r9   r   r   r#   r#      s     CHH---r   c                 6   ddl m} t          | t                    r|                                 S t          | t
                    r || dgd          S | "t                                          d          S t          d|  dt          |            d	          )
Nr   )r   r   r   F)r1   z\Argument `seed` must be either an integer or an instance of `SeedGenerator`. Received: seed=z
 (of type ))	keras.src.backendr   r$   r	   r5   r%   rB   r!   type)r   r   s     r   	draw_seedrN      s    333333$&& ;yy{{	D#		 ;  $(;;;;	$&&++E+:::
	8	8 	8*.t**	8 	8 	8  r   )randomrH   numpyr.   	keras.srcr   keras.src.api_exportr   keras.src.backend.commonr   keras.src.utilsr   keras.src.utils.namingr   r	   rB   r#   rN   r9   r   r   <module>rV      s                  - - - - - - 1 1 1 1 1 1 % % % % % % , , , , , , *++Z Z Z Z Z Z Z ,+Zz  0. . .    r   