
    2Vh\                     "   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
 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  G d d      Zd Zd Z eddg      d        Zd Zd Z ed      d        Z ed      d        Zd Z G d d      Zy)    Nbackend)keras_export)config)dtypes)global_state)current_path)get_stateless_scope)in_stateless_scope)
tensorflow)	auto_namec                      e Zd ZdZ	 	 	 	 	 	 	 dEdZd Zd Zd Zd Ze	d        Z
e	d	        Ze	d
        Zd Zd Zd Ze	d        Ze	d        Ze	d        Ze	d        Zej(                  d        Ze	d        Ze	d        Ze	d        Zej(                  d        Ze	d        Zej(                  d        Ze	d        Zej(                  d        Zd Zd Zd ZdFdZd Zd  Zd! Z dFd"Z!d# Z"d$ Z#d% Z$d& Z%d' Z&d( Z'd) Z(d* Z)d+ Z*d, Z+d- Z,d. Z-d/ Z.d0 Z/d1 Z0d2 Z1d3 Z2d4 Z3d5 Z4d6 Z5d7 Z6d8 Z7d9 Z8d: Z9d; Z:d< Z;d= Z<d> Z=d? Z>d@ Z?dA Z@dB ZAdC ZBdFdDZCy)GVariablea  Represents a backend-agnostic variable in Keras.

    A `Variable` acts as a container for state. It holds a tensor value and can
    be updated. With the JAX backend, variables are used to implement
    "functionalization", the pattern of lifting stateful operations out of
    a piece of computation to turn it into a stateless function.

    Args:
        initializer: Initial value or callable for initialization.
            If a callable is used, it should take the arguments
            `shape` and `dtype`.
        shape: Optional. Tuple for the variable's shape.
            Required if `initializer` is a callable.
        dtype: Optional. Data type of the variable. Defaults to the global float
            dtype type (`"float32"` if never configured).
        trainable: Optional. Boolean indicating if variable is trainable.
            Defaults to `True`.
        autocast: Optional. Boolean indicating whether the variable supports
            autocasting. If `True`, the layer may first convert the variable
            to the compute data type when accessed. Defaults to `True`.
        aggregation: Optional string, one of `None`, `"none"`, `"mean"`,
            `"sum"` or `"only_first_replica"` specifying how a distributed
            variable will be aggregated. This serves as a semantic annotation,
            to be taken into account by downstream backends or users. Defaults
            to `"none"`.
        name: Optional. A unique name for the variable. Automatically generated
            if not set.

    Attributes:
        shape: The shape of the variable (tuple of integers).
        ndim: The number of dimensions of the variable (integer).
        dtype: The data type of the variable (string).
        trainable: Whether the variable is trainable (boolean).
        autocast: Whether the variable supports autocasting (boolean).
        aggregation: How a distributed variable will be aggregated (string).
        value: The current value of the variable (NumPy array or tensor).
        name: The name of the variable (string).
        path: The path of the variable within the Keras model or layer (string).
        kwargs: Additional backend-specific keyword arguments.

    Examples:

    **Initializing a `Variable` with a NumPy array:**

    ```python
    import numpy as np
    import keras
    initial_array = np.ones((3, 3))
    variable_from_array = keras.Variable(initializer=initial_array)
    ```

    **Using a Keras initializer to create a `Variable`:**

    ```python
    from keras.src.initializers import Ones
    variable_from_initializer = keras.Variable(
        initializer=Ones(), shape=(3, 3), dtype="float32"
    )
    ```

    **Updating the value of a `Variable`:**

    ```python
    new_value = np.zeros((3, 3), dtype="float32")
    variable_from_array.assign(new_value)
    ```

    **Marking a `Variable` as non-trainable:**

    ```python
    non_trainable_variable = keras.Variable(
        initializer=np.ones((3, 3), dtype="float32"), trainable=False
    )
    ```
    Nc	                    ~	|xs t        | j                  j                        }t        |t              rd|v rt        d|       |dvrt        d|       |d}|dvrt        d|       |d}|| _        t               }
|
rt               dz   |z   | _        n|| _        d | _	        d | _
        d | _        d | _        t        |      | _        t        |      | _        || _        || _        d| _        t        |t              rd	d
lm} |j+                  |      }t-        |      r|2t        d| d|       | j/                  ||      }||j0                  }t3        |      | _        t7               rFt-        |      r0d | _        || _
        | j;                  |      | _	        t=        |        nyt        d      t-        |      r(| j;                  |      | _	        | j?                  |       n;| jA                  |       | j;                  | j8                  jB                        | _	        tE        | j                        | _#        y )N/zRArgument `name` must be a string and cannot contain character `/`. Received: name=)Nnonemeansumonly_first_replicazInvalid value for argument `aggregation`. Expected one of `None`, `'none'`, `'mean'`, `'sum'`, `'only_first_replica'`. Received: aggregation=r   )Nr   on_readon_writeautozInvalid value for argument `synchronization`. Expected one of `None`, `'none'`, `'on_read'`, `'on_write'`, `'auto'`. Received: synchronization=Fr   )initializersznWhen creating a Variable from an initializer, the `shape` argument should be specified. Received: initializer=z and shape=dtypead  You are attempting to create a variable while in a stateless scope. This is disallowed. Make sure that all variables are created before you start using your layer/model objects.

In some cases, you might be seeing this error because you need to implement a `def build(self, input_shape)` method on your layer/model, which will create its variables.

In some other cases, you might be seeing this error because you are instantiating a `Variable` and assigning it to a layer without going through self.add_variable()/self.add_weight(). Always prefer using these methods (with a `shape` and `initializer` argument).)$r   	__class____name__
isinstancestr
ValueError_namer	   _path_shape_initializer_regularizer_constraintbool
_trainable	_autocast_aggregation_synchronization_overwrite_with_gradient	keras.srcr   getcallable_convert_to_tensorr   standardize_dtype_dtyper   _value_validate_shaperegister_uninitialized_variable_initialize_with_initializer_initializeshapelen_ndim)selfinitializerr8   r   	trainableautocastaggregationsynchronizationnamekwargsparent_pathr   s               R/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/backend/common/variables.py__init__zVariable.__init__\   sn    9y!8!89$$t""&) 
  
 
 ) *57   K #
 
 - .=,=?  "$O
"n%#-4DJDJ  y/h' / ).%k3'.&**;7KK } --8M :!!&)  11+U1KK}#))'.$"$/!"2259/5 C $ $"225911+>  -"224;;3D3DE%
    c                     | j                   t        d| j                   d      t               rt        d      | j	                  | j
                         d | _        y )Nz	Variable z is already initialized.zYou are attempting to initialize a variable while in a stateless scope. This is disallowed. Make sure that all variables are initialized before you start using your layer/model objects.)r3   r    pathr   r6   r$   r;   s    rD   _deferred_initializezVariable._deferred_initialize   s\    ;;"y3KLMMC  	))$*;*;< rF   c                 \    t        |      }d |v rt        d| d| j                   d      |S )NzbShapes used to initialize variables must be fully-defined (no `None` dimensions). Received: shape=z for variable path='')standardize_shaper    rH   )r;   r8   s     rD   r4   zVariable._validate_shape   sD    !%(5=3DII;aA 
 rF   c                 X    t               }| j                  r||j                  |      S |S N)get_autocast_scoper)   
maybe_cast)r;   valueautocast_scopes      rD   _maybe_autocastzVariable._maybe_autocast   s,    +->>n8!,,U33rF   c                 ,    t        j                  |       S rO   )nparrayrI   s    rD   numpyzVariable.numpy   s    xx~rF   c                     | j                   S )z+The strategy for aggregating this variable.)r*   rI   s    rD   r?   zVariable.aggregation   s        rF   c                     | j                   S )z-The strategy for synchronizing this variable.)r+   rI   s    rD   r@   zVariable.synchronization   s     $$$rF   c                 ,   t               r.t               }|j                  |       }|| j                  |      S | j                  6| j                  | j                  | j                  | j                              S | j                  | j                        S )zBThe current value of the variable (numpy array or backend tensor).r   )r   r
   get_current_valuerT   r3   r$   r#   r2   )r;   scoperR   s      rD   rR   zVariable.value   s     ')E++D1E ++E22;;
 ''!!$++T[[!A  ##DKK00rF   c                 X   | j                  || j                        }t        |j                  | j                        s2t	        d| j
                  j                   d|j                   d|        t               rt               }|j                  | |f       |S | j                  |       |S )Nr   zzThe shape of the target variable and the shape of the target value in `variable.assign(value)` must match. variable.shape=z, Received: value.shape=z. Target variable: )
r0   r   shape_equalr8   r    rR   r   r
   
add_update_direct_assign)r;   rR   r]   s      rD   assignzVariable.assign  s    ''TZZ'@5;;

3" #'**"2"2!3 4)). 6$$(6+  ')EdE]+  &rF   c                 *    | j                  | |z         S rO   rb   r;   rR   s     rD   
assign_addzVariable.assign_add#      {{4%<((rF   c                 *    | j                  | |z
        S rO   rd   re   s     rD   
assign_subzVariable.assign_sub&  rg   rF   c                     t               }| j                  r$|"t        | j                        r|j                  }n| j                  }t        j                  |      S )zThe data type of the variable.)rP   r)   is_float_dtyper2   r   r   r1   )r;   rS   r   s      rD   r   zVariable.dtype)  sJ     ,-NN*t{{+"((EKKE((//rF   c                     | j                   S )zThe shape of the variable.)r#   rI   s    rD   r8   zVariable.shape7  s     {{rF   c                     | j                   S )z)The number of dimensions of the variable.)r:   rI   s    rD   ndimzVariable.ndim<       zzrF   c                     | j                   S )z"Whether the variable is trainable.)r(   rI   s    rD   r=   zVariable.trainableA  s     rF   c                 $    t        |      | _        y rO   )r'   r(   re   s     rD   r=   zVariable.trainableF  s    u+rF   c                     | j                   S )zThe name of the variable.)r!   rI   s    rD   rA   zVariable.nameJ  ro   rF   c                     | j                   S )z9The path of the variable within the Keras model or layer.)r"   rI   s    rD   rH   zVariable.pathO  ro   rF   c                     | j                   S )a  Whether this variable should be overwritten by the gradient.

        This property is designed for a special case where we want to overwrite
        the variable directly with its computed gradient. For example, in float8
        training, new `scale` and `amax_history` are computed as gradients, and
        we want to overwrite them directly instead of following the typical
        procedure such as gradient descent with a learning rate, gradient
        clipping and weight decaying.
        )r,   rI   s    rD   overwrite_with_gradientz Variable.overwrite_with_gradientT  s     ,,,rF   c                 N    t        |t              st        d|       || _        y )Nz7`overwrite_with_gradient` must be a boolean. Received: )r   r'   	TypeErrorr,   re   s     rD   ru   z Variable.overwrite_with_gradienta  s2    %&"G%  ).%rF   c                     | j                   S rO   )r%   rI   s    rD   regularizerzVariable.regularizerj  s       rF   c                 V    ddl m} |t        ||      st        d|       || _        y )Nr   )RegularizerzInvalid value for attribute `regularizer`. Expected an instance of `keras.regularizers.Regularizer`, or `None`. Received: regularizer=)keras.src.regularizersr{   r   r    r%   )r;   rR   r{   s      rD   ry   zVariable.regularizern  s:    6Z{%C)).1 
 "rF   c                     | j                   S rO   )r&   rI   s    rD   
constraintzVariable.constraintz  s    rF   c                 V    ddl m} |t        ||      st        d|       || _        y )Nr   )
ConstraintzInvalid value for attribute `constraint`. Expected an instance of `keras.constraints.Constraint`, or `None`. Received: constraint=)keras.src.constraintsr   r   r    r&   )r;   rR   r   s      rD   r~   zVariable.constraint~  s:    4Zz%B((-w0 
 !rF   c                     d }t        | d      r5| j                  )t        j                  j	                  | j                        }|d| nd}d| j
                   d| j                   d| j                   | dS )Nr3   z, value= z<Variable path=z, shape=z, dtype=>)hasattrr3   r   coreconvert_to_numpyrH   r8   r   )r;   rR   	value_strs      rD   __repr__zVariable.__repr__  sx    4"t{{'>LL11$++>E*/*;hug&	dii[ =ZZL1.	
rF   c                     t         rO   NotImplementedErrorre   s     rD   r7   zVariable._initialize      !!rF   c                     | j                   || j                  | j                              }| j                  |       y )Nr   )r0   r#   r2   r7   )r;   r<   rR   s      rD   r6   z%Variable._initialize_with_initializer  s4    ''4;;7
 	rF   c                     t         rO   r   )r;   rR   r   s      rD   r0   zVariable._convert_to_tensor  r   rF   c                 8    | j                   j                  |      S rO   )rR   __getitem__)r;   idxs     rD   r   zVariable.__getitem__  s    zz%%c**rF   c                 z    | j                   dkD  rt        d| j                         t        | j                        S Nr   zBOnly scalar arrays can be converted to Python scalars. Got: shape=)rn   rw   r8   intrR   rI   s    rD   __int__zVariable.__int__  s=    99q="jj\+  4::rF   c                 z    | j                   dkD  rt        d| j                         t        | j                        S r   )rn   rw   r8   floatrR   rI   s    rD   	__float__zVariable.__float__  s>    99q="jj\+  TZZ  rF   c                 ^    t        j                  | j                  j                  |            S rO   )rV   asarrayrR   	__array__r;   r   s     rD   r   zVariable.__array__  s"    
 zz$**..u566rF   c                     t        d      )Nz-A Keras Variable cannot be used as a boolean.)rw   rI   s    rD   __bool__zVariable.__bool__  s    GHHrF   c                 6    | j                   j                         S rO   )rR   __neg__rI   s    rD   r   zVariable.__neg__      zz!!##rF   c                     | j                   S rO   )rR   rI   s    rD   __pos__zVariable.__pos__  s    zzrF   c                 6    | j                   j                         S rO   )rR   __abs__rI   s    rD   r   zVariable.__abs__  r   rF   c                 6    | j                   j                         S rO   )rR   
__invert__rI   s    rD   r   zVariable.__invert__  s    zz$$&&rF   c                 V    t         j                  j                  | j                  |      S rO   )r   rX   equalrR   r;   others     rD   __eq__zVariable.__eq__      }}""4::u55rF   c                 V    t         j                  j                  | j                  |      S rO   )r   rX   	not_equalrR   r   s     rD   __ne__zVariable.__ne__  s    }}&&tzz599rF   c                 V    t         j                  j                  | j                  |      S rO   )r   rX   lessrR   r   s     rD   __lt__zVariable.__lt__  s    }}!!$**e44rF   c                 V    t         j                  j                  | j                  |      S rO   )r   rX   
less_equalrR   r   s     rD   __le__zVariable.__le__      }}''

E::rF   c                 V    t         j                  j                  | j                  |      S rO   )r   rX   greaterrR   r   s     rD   __gt__zVariable.__gt__  s    }}$$TZZ77rF   c                 V    t         j                  j                  | j                  |      S rO   )r   rX   greater_equalrR   r   s     rD   __ge__zVariable.__ge__  s    }}**4::u==rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   addrR   r   s     rD   __add__zVariable.__add__      }}  U33rF   c                 V    t         j                  j                  || j                        S rO   r   r   s     rD   __radd__zVariable.__radd__      }}  

33rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   subtractrR   r   s     rD   __sub__zVariable.__sub__      }}%%djj%88rF   c                 V    t         j                  j                  || j                        S rO   r   r   s     rD   __rsub__zVariable.__rsub__      }}%%eTZZ88rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   multiplyrR   r   s     rD   __mul__zVariable.__mul__  r   rF   c                 V    t         j                  j                  || j                        S rO   r   r   s     rD   __rmul__zVariable.__rmul__  r   rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   true_dividerR   r   s     rD   __truediv__zVariable.__truediv__      }}((U;;rF   c                 V    t         j                  j                  || j                        S rO   r   r   s     rD   __rtruediv__zVariable.__rtruediv__      }}((

;;rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   floor_dividerR   r   s     rD   __floordiv__zVariable.__floordiv__  s    }}))$**e<<rF   c                 V    t         j                  j                  || j                        S rO   r   r   s     rD   __rfloordiv__zVariable.__rfloordiv__  s    }}))%<<rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   modrR   r   s     rD   __mod__zVariable.__mod__  r   rF   c                 V    t         j                  j                  || j                        S rO   r   r   s     rD   __rmod__zVariable.__rmod__  r   rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   powerrR   r   s     rD   __pow__zVariable.__pow__  r   rF   c                 V    t         j                  j                  || j                        S rO   r   r   s     rD   __rpow__zVariable.__rpow__  s    }}""5$**55rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   matmulrR   r   s     rD   
__matmul__zVariable.__matmul__  s    }}##DJJ66rF   c                 V    t         j                  j                  || j                        S rO   r   r   s     rD   __rmatmul__zVariable.__rmatmul__  s    }}##E4::66rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   logical_andrR   r   s     rD   __and__zVariable.__and__  r   rF   c                 V    t         j                  j                  || j                        S rO   r   r   s     rD   __rand__zVariable.__rand__  r   rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   
logical_orrR   r   s     rD   __or__zVariable.__or__  r   rF   c                 V    t         j                  j                  || j                        S rO   r   r   s     rD   __ror__zVariable.__ror__  s    }}''tzz::rF   c                 V    t         j                  j                  | j                  |      S rO   r   rX   logical_xorrR   r   s     rD   __xor__zVariable.__xor__  r   rF   c                 V    t         j                  j                  || j                        S rO   r  r   s     rD   __rxor__zVariable.__rxor__  r   rF   c                 d    |xs d}t         j                  j                  | j                  |      S )Nr   )decimals)r   rX   roundrR   )r;   ndigitsr  s      rD   	__round__zVariable.__round__  s)    <a}}""4::"AArF   )NNTTr   r   NrO   )Dr   
__module____qualname____doc__rE   rJ   r4   rT   rX   propertyr?   r@   rR   rb   rf   ri   r   r8   rn   r=   setterrA   rH   ru   ry   r~   r   r7   r6   r0   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   rF   rD   r   r      sv   J^ v&p! ! ! % % 1 1"$)) 0 0       & &     
- 
- ##. $. ! ! 	" 	"     	! 	!
" "+!7I$$'6:5;8>449999<<==446677<<;;<<BrF   r   c                 V    t        j                  dg d      }|j                  |        y )Nuninitialized_variablesT)set_to_default)r   get_global_attributeappend)variabler  s     rD   r5   r5   "  s*    *??!2d ""8,rF   c                      t        j                  d      } | r| D ]  }|j                           t        j                  dg        y )Nr  )r   r  rJ   set_global_attribute)
collectionvs     rD   initialize_all_variablesr  )  sB    223LMJ 	%A""$	%%%&?DrF   zkeras.utils.standardize_dtypezkeras.backend.standardize_dtypec                    | t        j                         S t        j                  j	                  | |       } t        | d      r| j                  } n\t        | d      r| j                  } nCt        | d      r7dt        |       v sdt        |       v rt        |       j                  d      d   } | t        j                  vrt        d|        | S )	NrA   r   __str__torchz	jax.numpy.zInvalid dtype: )r   floatxr   PYTHON_DTYPES_MAPr.   r   rA   r   r   splitALLOWED_DTYPESr    r   s    rD   r1   r1   1  s     }}}$$((6Euf

	
	#			"3u:E
!:E
  %b)F)))?5'233LrF   c                 d   t        | t              st| t        d      t        | d      st        d|  d      t	        j
                         dk(  r*t        | t        j                        r| j                         } t        |       } t	        j
                         dk(  rt        t        d |             } | D ]y  }|t	        j
                         dk(  rd	t        t        |            v r4t        t        |            st        d|  d
| dt        |       d      |dk  slt        d|  d       | S )Nz#Undefined shapes are not supported.__iter__zCannot convert 'z' to a shape.r   r  c                      | t        |       S d S rO   )r   )xs    rD   <lambda>z#standardize_shape.<locals>.<lambda>V  s    amCF  rF   jax_DimExprz#' to a shape. Found invalid entry 'z' of type 'z'. r   z2' to a shape. Negative dimensions are not allowed.)r   tupler    r   r   r   tfTensorShapeas_listmapr   typeis_int_dtype)r8   es     rD   rM   rM   F  s4   eU#=BCCuj)/wmDEE>>|+%0 e~~7" cEuMN 9>>u$s47|)CDG$"5' *(()s+d1gYcC  q5"5' *7 7   LrF   c                 v    t        |       t        |      k7  ryt        | |      D ]  \  }}|	|||k7  s y y)z8Return whether a_shape == b_shape (allows None entries).FT)r9   zip)a_shapeb_shapee1e2s       rD   r_   r_   k  sF    
7|s7|#gw' B>bnr rF   zkeras.backend.is_float_dtypec                 `    t        |       } | j                  d      xs | j                  d      S )Nr   bfloatr1   
startswithr   s    rD   rk   rk   u  s-    e$EG$B(8(8(BBrF   zkeras.backend.is_int_dtypec                 `    t        |       } | j                  d      xs | j                  d      S )Nr   uintr=  r   s    rD   r3  r3  {  s-    e$EE">e&6&6v&>>rF   c                  ,    t        j                  d      S NrS   )r   r  r  rF   rD   rP   rP     s    ,,-=>>rF   c                   (    e Zd ZdZd Zd Zd Zd Zy)AutocastScopezContext manager that enables the autocasting of float variables.

    Under this context manager, float `Variables`s will be cast to `dtype`
    (note that `dtype` must also be float).
    c                 l    |$t        |      }t        |      st        d|       || _        d | _        y )Nzh`AutocastScope` can only be used with a floating-point target dtype, such as 'float16'. Received: dtype=)r1   rk   r    r   original_scoper   s     rD   rE   zAutocastScope.__init__  sG    %e,E!%( '',g/ 
 
"rF   c                     ddl m} | j                  3t        |j                        r |j                  || j                        S |S )Nr   r   r   )r-   r   r   rk   cast)r;   rR   r   s      rD   rQ   zAutocastScope.maybe_cast  s6    %::!nU[[&A7<<TZZ88rF   c                 N    t               | _        t        j                  d|        y rB  )rP   rF  r   r  rI   s    rD   	__enter__zAutocastScope.__enter__  s    02))*:DArF   c                 D    t        j                  d| j                         y rB  )r   r  rF  )r;   argsrB   s      rD   __exit__zAutocastScope.__exit__  s    ))*:D<O<OPrF   N)r   r  r  r  rE   rQ   rJ  rM  r  rF   rD   rD  rD    s    
#BQrF   rD  )rX   rV   r-   r   keras.src.api_exportr   keras.src.backendr   keras.src.backend.commonr   r   #keras.src.backend.common.name_scoper	   (keras.src.backend.common.stateless_scoper
   r   keras.src.utils.module_utilsr   r.  keras.src.utils.namingr   r   r5   r  r1   rM   r_   rk   r3  rP   rD  r  rF   rD   <module>rU     s      - $ + 1 < H G 9 ,PB PBf-E $&GH$"J ,-C .C
 *+? ,?
?Q QrF   