o
    2hS                     @   sB   d dl mZ d dlmZ edG dd dZdd Zdd	 Zd
S )    )keras_export)global_statezkeras.StatelessScopec                   @   sH   e Zd ZdZ			dddZdd Zd	d
 Zdd Zdd Zd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 `Variable`
    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              	   C   s   ddl m} ddlm} || _|| _g | _i | _|pi }|D ]C\}}t||s.t	d| t||r=|j
|j|jd}n|j||jd}|j|jkrZt	d| d| d|j d	|| jt|< qd S )
Nr   )backend)Variablez{Invalid reference variable in StatelessScope: all keys in argument `mapping` must be Variable 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v r   c/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/backend/common/stateless_scope.py__init__#   s:   

zStatelessScope.__init__c                 C   s   t  | _td|  | S Nstateless_scope)get_stateless_scopeoriginal_scoper   set_global_attribute)r   r   r   r   	__enter__F   s   zStatelessScope.__enter__c                 C   s   | j | d S N)r   append)r   lossr   r   r   add_lossK   s   zStatelessScope.add_lossc                 C   s   |\}}|| j t|< d S r"   )r   r   )r   updatevariabler   r   r   r   
add_updateN   s   zStatelessScope.add_updatec                 C   s   | j t|d S r"   )r   getr   )r   r'   r   r   r   get_current_valueR   s   z StatelessScope.get_current_valuec                 O   s<   t d| j | jd u r| jrddlm} |  d S d S d S )Nr   r   )initialize_all_variables)r   r    r   r   r
   r+   )r   argskwargsr+   r   r   r   __exit__U   s   
zStatelessScope.__exit__)NFT)
__name__
__module____qualname____doc__r   r!   r%   r(   r*   r.   r   r   r   r   r      s    
#r   c                   C   s   t dd uS r   r   get_global_attributer   r   r   r   in_stateless_scoped   s   r5   c                   C   s
   t dS r   r3   r   r   r   r   r   h   s   
r   N)keras.src.api_exportr   keras.src.backend.commonr   r   r5   r   r   r   r   r   <module>   s    ^