
    &Vfl                     f    d dl mZ d dlmZ  ed           G d d                      Zd Zd ZdS )	    )keras_export)global_statezkeras.StatelessScopec                   >    e Zd ZdZ	 	 	 ddZd Zd Zd Zd	 Zd
 Z	dS )StatelessScopea  Scope to prevent any update to Keras Variables.

    The values of variables to be used inside the scope
    should be passed via the `state_mapping` argument, a
    list of tuples `(k, v)` where `k` is a `KerasVariable`
    and `v` is the intended value for this variable
    (a backend tensor).

    Updated values can be collected on scope exit via
    `value = scope.get_current_value(variable)`. No updates
    will be applied in-place to any variables for the duration
    of the scope.

    Example:

    ```python
    state_mapping = [(k, ops.ones(k.shape, k.dtype)) for k in model.weights]
    with keras.StatelessScope(state_mapping) as scope:
        outputs = model.some_function(inputs)

    # All model variables remain unchanged. Their new values can be
    # collected via:
    for k in model.weights:
        new_value = scope.get_current_value(k)
        print(f"New value for {k}: {new_value})
    ```
    NFTc           
         ddl m} ddlm} || _        || _        g | _        i | _        |pi }|D ]\  }}t          ||          st          d|           t          ||          r"|
                    |j        |j                  }n|                    ||j                  }|j        |j        k    rt          d| d| d|j         d	          || j        t          |          <   d S )
Nr   )backend)KerasVariablezInvalid reference variable in StatelessScope: all keys in argument `mapping` must be KerasVariable instances. Received instead: )dtypezInvalid variable value in StatelessScope: all values in argument `mapping` must be tensors with a shape that matches the corresponding variable shape. For variable z, received invalid value z with shape .)	keras.srcr   "keras.src.backend.common.variablesr	   collect_lossesinitialize_variableslossesstate_mapping
isinstance
ValueErrorcastvaluer
   convert_to_tensorshapeid)selfr   r   r   r   r	   kvs           e/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/backend/common/stateless_scope.py__init__zStatelessScope.__init__#   sb    	&%%%%%DDDDDD,$8!%+! 	* 	*DAqa//  8458 8  
 !]++ @LLL88--aqw-??w!'!! " %&" " AB" " w	" " "   )*Dr!uu%%'	* 	*    c                 V    t                      | _        t          j        d|            | S Nstateless_scope)get_stateless_scopeoriginal_scoper   set_global_attribute)r   s    r   	__enter__zStatelessScope.__enter__F   s)    133)*;TBBBr   c                 :    | j                             |           d S N)r   append)r   losss     r   add_losszStatelessScope.add_lossK   s    4     r   c                 >    |\  }}|| j         t          |          <   d S r'   )r   r   )r   updatevariabler   s       r   
add_updatezStatelessScope.add_updateN   s$     %+02h<<(((r   c                 R    | j                             t          |          d           S r'   )r   getr   )r   r-   s     r   get_current_valuez StatelessScope.get_current_valueR   s!    !%%bllD999r   c                 ~    t          j        d| j                   | j        | j        rddlm}  |             d S d S d S )Nr!   r   )initialize_all_variables)r   r$   r#   r   r   r3   )r   argskwargsr3   s       r   __exit__zStatelessScope.__exit__U   su    )t2	
 	
 	
 &4+D&      %$&&&&& '&&&r   )NFT)
__name__
__module____qualname____doc__r   r%   r*   r.   r1   r6    r   r   r   r      s         < !	!* !* !* !*F  
! ! !1 1 1: : :' ' ' ' 'r   r   c                  .    t          j        d          d uS r    r   get_global_attributer;   r   r   in_stateless_scoper?   d   s    ,->??tKKr   c                  *    t          j        d          S r    r=   r;   r   r   r"   r"   h   s    ,->???r   N)keras.src.api_exportr   keras.src.backend.commonr   r   r?   r"   r;   r   r   <module>rC      s    - - - - - - 1 1 1 1 1 1 $%%[' [' [' [' [' [' [' &%['|L L L@ @ @ @ @r   