
    2Vh+                    :   d Z ddlZddlZddlZ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 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 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. ddl,m/Z/ ddl,m0Z0  ej                         dk(  rddl1m2Z3 n ej                         d k(  rdd!l4m5Z3 nl ej                         d"k(  rdd#l6m7Z3 nQ ej                         d$k(  rdd%l8m9Z3 n6 ej                         d&k(  rdd'l:m;Z3 n e<d( ej                          d)       ed*d+g       G d, d-e3e)e+             Z=d8d.Z> G d/ d0      Z?d1 Z@d2 ZAd3 ZB G d4 d5      ZCd6 ZDd7 ZEy)9a  Layer is an Operation with state.

Takes care of:

- Weights / variables (and tracking thereof)
- deferred build
- trainable argument value inference
- masking
- autocasting

And some more magic:

- add_loss
- metric tracking
- RNG seed tracking
- activity regularization
    N)wraps)backend)constraints)dtype_policies)initializers)regularizers)tree)utils)keras_export)KerasTensor)global_state)remat)any_symbolic_tensors)current_path)get_current_remat_mode)in_symbolic_scope)distribution_lib)DTypePolicyMap)
input_spec)Metric)Node)	Operation)KerasSaveable)python_utils)summary_utils)traceback_utils)tracking
tensorflow)TFLayerjax)JaxLayertorch)
TorchLayernumpy)
NumpyLayeropenvino)OpenvinoLayerz	Backend 'z%' must implement a layer mixin class.zkeras.Layerzkeras.layers.Layerc                   P    e Zd ZdZ fdZdddddddZej                  d        Zd Z	e
d	        Ze
d
        Zej                  d        Zej                  d        Zd Zd Zd Zd Z	 	 	 	 	 	 dVdZ	 	 	 	 	 	 	 	 	 	 dWdZe
d        Zej                  d        Ze
d        Ze
d        Ze
d        Ze
d        Ze
d        Ze
d        Ze
d        Ze
d        Zd Z d Z!e
d         Z"e"j                  d!        Z"e
d"        Z#e
d#        Z$e
d$        Z%e
d%        Z&e
d&        Z'e
d'        Z(e(j                  d(        Z(ej                  d)        Z)d* Z*e+jX                   fd+       Z-d, Z.d- Z/e+jX                  dd.d/       Z0 fd0Z1ej                  d1        Z2d2 Z3d3 Z4d4 Z5e
d5        Z6d6 Z7d7 Z8dXd8Z9d9 Z:d: Z;d; Z<d< Z=dYd=Z>d> Z?d? Z@d@ ZAdA ZBdB ZCdC ZDdD ZEdE ZFdF ZGdG ZHdH ZIdI ZJ fdJZK fdKZLdL ZMdM ZNdN ZOdO ZPdZdPZQdQ ZReSj                   fdR       ZTdS ZUdT ZVdU ZW xZXS )[Layera%  This is the class from which all layers inherit.

    A layer is a callable object that takes as input one or more tensors and
    that outputs one or more tensors. It involves *computation*, defined
    in the `call()` method, and a *state* (weight variables). State can be
    created:

    * in `__init__()`, for instance via `self.add_weight()`;
    * in the optional `build()` method, which is invoked by the first
      `__call__()` to the layer, and supplies the shape(s) of the input(s),
      which may not have been known at initialization time.

    Layers are recursively composable: If you assign a Layer instance as an
    attribute of another Layer, the outer layer will start tracking the weights
    created by the inner layer. Nested layers should be instantiated in the
    `__init__()` method or `build()` method.

    Users will just instantiate a layer and then treat it as a callable.

    Args:
        trainable: Boolean, whether the layer's variables should be trainable.
        name: String name of the layer.
        dtype: The dtype of the layer's computations and weights. Can also be a
            `keras.DTypePolicy`, which allows the computation and weight dtype
            to differ. Defaults to `None`. `None` means to use
            `keras.config.dtype_policy()`, which is a `float32` policy unless
            set to different value (via `keras.config.set_dtype_policy()`).

    Attributes:
        name: The name of the layer (string).
        dtype: Dtype of the layer's weights. Alias of `layer.variable_dtype`.
        variable_dtype: Dtype of the layer's weights.
        compute_dtype: The dtype of the layer's computations.
            Layers automatically cast inputs to this dtype, which causes
            the computations and output to also be in this dtype.
            When mixed precision is used with a
            `keras.DTypePolicy`, this will be different
            than `variable_dtype`.
        trainable_weights: List of variables to be included in backprop.
        non_trainable_weights: List of variables that should not be
            included in backprop.
        weights: The concatenation of the lists trainable_weights and
            non_trainable_weights (in this order).
        trainable: Whether the layer should be trained (boolean), i.e.
            whether its potentially-trainable weights should be returned
            as part of `layer.trainable_weights`.
        input_spec: Optional (list of) `InputSpec` object(s) specifying the
            constraints on inputs that can be accepted by the layer.

    We recommend that descendants of `Layer` implement the following methods:

    * `__init__()`: Defines custom layer attributes, and creates layer weights
        that do not depend on input shapes, using `add_weight()`,
        or other state.
    * `build(self, input_shape)`: This method can be used to create weights that
        depend on the shape(s) of the input(s), using `add_weight()`, or other
        state. `__call__()` will automatically build the layer
        (if it has not been built yet) by calling `build()`.
    * `call(self, *args, **kwargs)`: Called in `__call__` after making
        sure `build()` has been called. `call()` performs the logic of applying
        the layer to the input arguments.
        Two reserved keyword arguments you can optionally use in `call()` are:
            1. `training` (boolean, whether the call is in inference mode or
                training mode).
            2. `mask` (boolean tensor encoding masked timesteps in the input,
                used e.g. in RNN layers).
        A typical signature for this method is `call(self, inputs)`, and user
        could optionally add `training` and `mask` if the layer need them.
    * `get_config(self)`: Returns a dictionary containing the configuration
        used to initialize this layer. If the keys differ from the arguments
        in `__init__()`, then override `from_config(self)` as well.
        This method is used when saving
        the layer or a model that contains this layer.

    Examples:

    Here's a basic example: a layer with two variables, `w` and `b`,
    that returns `y = w . x + b`.
    It shows how to implement `build()` and `call()`.
    Variables set as attributes of a layer are tracked as weights
    of the layers (in `layer.weights`).

    ```python
    class SimpleDense(Layer):
        def __init__(self, units=32):
            super().__init__()
            self.units = units

        # Create the state of the layer (weights)
        def build(self, input_shape):
            self.kernel = self.add_weight(
                shape=(input_shape[-1], self.units),
                initializer="glorot_uniform",
                trainable=True,
                name="kernel",
            )
            self.bias = self.add_weight(
                shape=(self.units,),
                initializer="zeros",
                trainable=True,
                name="bias",
            )

        # Defines the computation
        def call(self, inputs):
            return ops.matmul(inputs, self.kernel) + self.bias

    # Instantiates the layer.
    linear_layer = SimpleDense(4)

    # This will also call `build(input_shape)` and create the weights.
    y = linear_layer(ops.ones((2, 2)))
    assert len(linear_layer.weights) == 2

    # These weights are trainable, so they're listed in `trainable_weights`:
    assert len(linear_layer.trainable_weights) == 2
    ```

    Besides trainable weights, updated via backpropagation during training,
    layers can also have non-trainable weights. These weights are meant to
    be updated manually during `call()`. Here's a example layer that computes
    the running sum of its inputs:

    ```python
    class ComputeSum(Layer):

      def __init__(self, input_dim):
          super(ComputeSum, self).__init__()
          # Create a non-trainable weight.
          self.total = self.add_weight(
            shape=(),
            initializer="zeros",
            trainable=False,
            name="total",
          )

      def call(self, inputs):
          self.total.assign(self.total + ops.sum(inputs))
          return self.total

    my_sum = ComputeSum(2)
    x = ops.ones((2, 2))
    y = my_sum(x)

    assert my_sum.weights == [my_sum.total]
    assert my_sum.non_trainable_weights == [my_sum.total]
    assert my_sum.trainable_weights == []
    ```
    c                     t        |   | g|i |j                  t              fd       }|_        j                  t              fd       }|_        S )Nc                  8   j                         5  t               _         | i | d d d        t        j                        } |j
                  | i |j                  _        d_        j                          j                          y # 1 sw Y   gxY wNT)_open_name_scoper   _pathinspect	signaturebind	arguments_build_shapes_dictbuilt_post_build_lock_state)argskwargsr0   objoriginal_build_methods      F/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/layer.pybuild_wrapperz$Layer.__new__.<locals>.build_wrapper   s    %%' 7(N	%t6v67  ))*?@I%3Y^^T%DV%D%N%NC"CIOOOO7 7s   BBc                    j                  | j                         j                  j                          	  | fi | 	 j                  j                          y # t        $ r  w xY w# j                  j                          w xY wN)_check_quantize_argscompute_dtype_trackerunlock	Exceptionlock)moder8   r9   original_quantize_methods     r;   quantize_wrapperz'Layer.__new__.<locals>.quantize_wrapper   st    $$T3+<+<=LL!$(88 !!#   !!#s   	A A))A, ,B)super__new__buildr   quantize)	clsr7   r8   r<   rG   r9   r:   rF   	__class__s	        @@@r;   rI   zLayer.__new__   sz    goc3D3F3 !$			$	%
	 
&
	 "	 $'<< 	'	(	$ 
)	$ (
    NT)activity_regularizer	trainabledtypeautocastnamec                p   t        j                  |        d| _        t        j                  | ||       t	        j
                  |      | _        |j                  dd       }||f}n|j                  dd       }|t        j                  dd       || _
        |r%t        d| j                  j                   d	|       d | _        d| _        || _        d | _        d| _        d
| _        || _        g | _        t-               | _        g | _        t3        j4                  | j6                        | _        | j8                  j:                  j=                         D 	cg c]  }	|	j>                   c}	| _         d| j@                  v | _!        d| j@                  v | _"        dh| _#        | jF                  D 
ci c]  }
|
|
| j@                  v  c}
| _$        tK        jL                  | jN                         | _(        d
| _)        d| _*        d | _+        d | _,        t[               | _.        | j_                          y c c}	w c c}
w )NF)rQ   rS   	input_diminput_shapezDo not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.   )
stacklevelz)Unrecognized keyword arguments passed to z: Ttrainingmask)0BackendLayer__init___lockr   r   getrO   popwarningswarn_input_shape_arg
ValueErrorrM   __name__r.   r4   rR   _input_spec_calledsupports_jit
_trainable_lossesset	_loss_ids_losses_overrider/   r0   call_call_signature
parametersvaluesrS   call_signature_parameters_call_has_training_arg_call_has_mask_arg_call_context_args_call_has_context_argr
   
is_defaultcompute_mask_supports_masking_convert_input_args!_allow_non_tensor_positional_argsr3   _parent_pathr   _remat_mode_initialize_tracker)selfrO   rP   rQ   rR   rS   r8   input_dim_arginput_shape_argpargs              r;   r\   zLayer.__init__  s$    	d#
4u48$0$4$45I$J!

;5$,.O$jj=O&MM4  %4D!!^^445RxA 
 

  # "&00; 00;;BBD*
AFF*
& $888 	# #)D,J,J"J $.,
 ..&
 #7777&
"
 &+%5%5d6G6G%H!H#' 16."& 13  "7*
&
s   H./H3c                 t   t        | d      ry g }g }g }g }g }t        j                  d |fd |fd |fd |fd |fddd	gi
      | _        t	        j                         dk(  rt        | dd      }d| _        || _        || _        || _	        || _
        || _        t	        j                         dk(  r| _        y y )NrA   c                 R    t        | t        j                        xr | j                  S r>   
isinstancer   VariablerP   xs    r;   <lambda>z+Layer._initialize_tracker.<locals>.<lambda>_  s    jG,<,<=M!++ rN   c                 T    t        | t        j                        xr | j                   S r>   r   r   s    r;   r   z+Layer._initialize_tracker.<locals>.<lambda>c  s$    jG,<,<= (KK rN   c                 "    t        | t              S r>   )r   r   r   s    r;   r   z+Layer._initialize_tracker.<locals>.<lambda>g  s    jF&; rN   c                 H    t        | t              xr t        | t               S r>   )r   r)   r   r   s    r;   r   z+Layer._initialize_tracker.<locals>.<lambda>i  s"    jE2 2&q&11 rN   c                 J    t        | t        j                  j                        S r>   )r   r   randomSeedGeneratorr   s    r;   r   z+Layer._initialize_tracker.<locals>.<lambda>n  s    jGNN,H,HI rN   )trainable_variablesnon_trainable_variablesmetricslayersseed_generatorsr   r   )
exclusionsr   _self_setattr_trackingTF)hasattrr   TrackerrA   r   getattrr   _trainable_variables_non_trainable_variables_layers_metrics_seed_generators)r~   r   r   r   r   r   r   s          r;   r}   zLayer._initialize_trackerR  s    4$ "$ (( N'(
(+,
 <WE2 J#$!* 24I3JK-
0 ??,%,.&" +0D'$7!(?% /??,*@D' -rN   c                     t        j                  | j                        r(d| _        | j	                          | j                          yy)zBuild the layer at `Layer.__init__`.

        We can only safely mark the layer as `built=True` in `Layer.__init__` if
        `build` is not overridden. Otherwise, it might cause the subclasses to
        ignore the user's `build`.
        TN)r
   rv   rJ   r4   r5   r6   r~   s    r;   _build_at_initzLayer._build_at_init  s:     DJJ'DJ (rN   c                     | j                   S )z`The path of the layer.

        If the layer has not been built yet, it will be `None`.
        )r.   r   s    r;   pathz
Layer.path  s     zzrN   c                     | j                   S r>   re   r   s    r;   r   zLayer.input_spec  s    rN   c                     || _         y r>   r   r~   values     r;   r   zLayer.input_spec  s
     rN   c                     | j                          t        j                  | j                        r.t	        |       r#t        j                  d| j                   d       d| _        y )Nz`build()` was called on layer 'a  ', however the layer does not have a `build()` method implemented and it looks like it has unbuilt state. This will cause the layer to be marked as built, despite not being actually built, which may cause failures down the line. Make sure to implement a proper `build()` method.T)	_check_super_calledr
   rv   rJ   might_have_unbuilt_stater`   ra   rS   r4   )r~   rV   s     r;   rJ   zLayer.build  sT      "DJJ',DT,JMM1$)) =D D 
rN   c                 j    | j                   j                  s| j                   j                  d       yy)zAPrevent further state updates, called automatically in `build()`.zYou cannot add new elements of state (variables or sub-layers) to a layer that is already built. All state must be created in the `__init__()` method or in the `build()` method.)msgN)rA   lockedrD   r   s    r;   r6   zLayer._lock_state  s0    }}##MM/   $rN   c                     | j                   Nt        | j                         dk(  r(dt        | j                   j                               d   iS d| j                   iS y)a  Returns a dictionary with the layer's input shape.

        This method returns a config dict that can be used by
        `build_from_config(config)` to create all states (e.g. Variables and
        Lookup tables) needed by the layer.

        By default, the config only contains the input shape that the layer
        was built with. If you're writing a custom layer that creates state in
        an unusual way, you should override this method to make sure this state
        is already created when Keras attempts to load its value upon model
        loading.

        Returns:
            A dict containing the input shape associated with the layer.
        N   rV   r   shapes_dict)r3   lentuplerp   r   s    r;   get_build_configzLayer.get_build_config  sc      "".4**+q0!5)@)@)G)G)I#J1#M  &t'>'>?? /rN   c                 p    |r4d|v r| j                  |d          yd|v r | j                   di |d    yyy)a  Builds the layer's states with the supplied config dict.

        By default, this method calls the `build(config["input_shape"])` method,
        which creates weights based on the layer's input shape in the supplied
        config. If your config contains other information needed to load the
        layer's state, you should override this method.

        Args:
            config: Dict containing the input shape associated with this layer.
        rV   r   N )rJ   )r~   configs     r;   build_from_configzLayer.build_from_config  sH     &

6-01&(

3VM23 ) rN   c                      y)Nr)   r   r   s    r;   	_obj_typezLayer._obj_type  s    rN   c	           
      4    | j                  ||||||||      S )zNAdd a weight variable to the layer.

        Alias of `add_weight()`.
        )shapeinitializerrQ   rP   rR   regularizer
constraintrS   )
add_weight)	r~   r   r   rQ   rP   rR   r   r   rS   s	            r;   add_variablezLayer.add_variable  s2     ##!  	
 		
rN   Fc           
         | j                          |d}|t        j                  |      }n| j                  }|	d|v rd}nd}t	        j
                  |      }t        j                  | j                  |       5  t        j                  |||||||
      }ddd       t        j
                  |      _
        t        j
                  |      |_        |	|_        | j                  |       |S # 1 sw Y   WxY w)a.  Add a weight variable to the layer.

        Args:
            shape: Shape tuple for the variable. Must be fully-defined
                (no `None` entries). Defaults to `()` (scalar) if unspecified.
            initializer: Initializer object to use to populate the initial
                variable value, or string name of a built-in initializer
                (e.g. `"random_normal"`). If unspecified, defaults to
                `"glorot_uniform"` for floating-point variables and to `"zeros"`
                for all other types (e.g. int, bool).
            dtype: Dtype of the variable to create, e.g. `"float32"`. If
                unspecified, defaults to the layer's variable dtype
                (which itself defaults to `"float32"` if unspecified).
            trainable: Boolean, whether the variable should be trainable via
                backprop or whether its updates are managed manually. Defaults
                to `True`.
            autocast: Boolean, whether to autocast layers variables when
                accessing them. Defaults to `True`.
            regularizer: Regularizer object to call to apply penalty on the
                weight. These penalties are summed into the loss function
                during optimization. Defaults to `None`.
            constraint: Contrainst object to call on the variable after any
                optimizer update, or string name of a built-in constraint.
                Defaults to `None`.
            aggregation: Optional string, one of `None`, `"none"`, `"mean"`,
                `"sum"` or `"only_first_replica"`. Annotates the variable with
                the type of multi-replica aggregation to be used for this
                variable when writing custom data parallel training loops.
                Defaults to `"none"`.
            overwrite_with_gradient: Boolean, whether to overwrite the variable
                with the computed gradient. This is useful for float8 training.
                Defaults to `False`.
            name: String name of the variable. Useful for debugging purposes.
        Nr   floatglorot_uniformzeroscaller)r   r   rQ   rP   rR   aggregationrS   )r   r   standardize_dtypevariable_dtyper   r^   
name_scoperS   r   r   r   r   r   overwrite_with_gradient_track_variable)r~   r   r   rQ   rP   rR   r   r   r   r   rS   variables               r;   r   zLayer.add_weight  s    ^ 	  "=E--e4E''E%.%"&&{3		$7 		'''#!'H		  ,//<)ooj9+B(X&		 		s   :C--C6c                     | j                   S )z@Settable boolean, whether this layer should be trainable or not.)rh   r   s    r;   rP   zLayer.trainableO  s     rN   c                     t        |      }|| _        | j                  D ]	  }||_         | j                  D ]	  }||_         y)a  Sets trainable attribute for the layer and its sublayers.

        When this value is changed during training (e.g. with a
        `Callback`) you need to call the parent
        `Model.make_train_function` with `force=True` in order to
        recompile the training graph.

        Args:
            value: Boolean with the desired state for the layer's trainable
                attribute.
        N)boolrh   r   rP   r   )r~   r   vlayers       r;   rP   zLayer.trainableT  sJ     U** 	 AAK	 \\ 	$E#EO	$rN   c                    g }t               }| j                  | j                  z   D ];  }t        |      |vs|j	                  |       |j                  t        |             = | j                  D ]  }|j	                  |j                          | j                  D ]L  }|j                  D ];  }t        |      |vs|j	                  |       |j                  t        |             = N |S )a)  List of all layer state, including random seeds.

        This extends `layer.weights` to include all state used by the layer
        including `SeedGenerator`s.

        Note that metrics variables are not included here, use
        `metrics_variables` to visit all the metric variables.
        )
rj   r   r   idappendaddr   stater   	variables)r~   r   seen_idsr   sgr   s         r;   r   zLayer.variablesh  s     	5**T-J-JJ 	$A!uH$  #RU#	$ '' 	'BRXX&	'\\ 	(E__ (a5($$Q'LLA'(	(
 rN   c                 v    | j                   sg S | j                  D cg c]  }|j                   s| c}S c c}w )zeList of all trainable layer state.

        This is equivalent to `layer.trainable_weights`.
        rP   r   r~   r   s     r;   r   zLayer.trainable_variables  s.     ~~I>>9aQ[[999   66c                     | j                   s| j                  S | j                  D cg c]  }|j                   r| c}S c c}w )zList of all non-trainable layer state.

        This extends `layer.non_trainable_weights` to include all state used by
        the layer including state for metrics and `SeedGenerator`s.
        r   r   s     r;   r   zLayer.non_trainable_variables  s3     ~~>>!>>=a===
   A A c                    g }t               }| j                  | j                  z   D ];  }t        |      |vs|j	                  |       |j                  t        |             = | j                  D ]L  }|j                  D ];  }t        |      |vs|j	                  |       |j                  t        |             = N |S )zList of all weight variables of the layer.

        Unlike, `layer.variables` this excludes metric state and random seeds.
        )rj   r   r   r   r   r   r   weights)r~   r   r   wr   s        r;   r   zLayer.weights  s     5**T-J-JJ 	$A!uH$q!RU#	$ \\ 	(E]] (a5(NN1%LLA'(	(
 rN   c                 v    | j                   sg S | j                  D cg c]  }|j                   s| c}S c c}w )zList of all trainable weight variables of the layer.

        These are the weights that get updated by the optimizer during training.
        rP   r   r   s     r;   trainable_weightszLayer.trainable_weights  s.     ~~I<<7a1;;777r   c                     | j                   s| j                  S | j                  D cg c]  }|j                   r| c}S c c}w )a  List of all non-trainable weight variables of the layer.

        These are the weights that should not be updated by the optimizer during
        training. Unlike, `layer.non_trainable_variables` this excludes metric
        state and random seeds.
        r   r   s     r;   non_trainable_weightszLayer.non_trainable_weights  s3     ~~<<<<;aq{{;;;r   c                     t        | j                        }| j                  D ]  }|j                  |j                          |S )zList of all metrics.)listr   r   extendr   )r~   r   r   s      r;   r   zLayer.metrics  s:     t}}%\\ 	*ENN5==)	*rN   c                 b    g }| j                   D ]  }|j                  |j                          |S )zList of all metric variables.)r   r   r   )r~   varsmetrics      r;   metrics_variableszLayer.metrics_variables  s3     ll 	*FKK(()	*rN   c                 \    | j                   D cg c]  }|j                          c}S c c}w )z?Return the values of `layer.weights` as a list of NumPy arrays.)r   r$   r   s     r;   get_weightszLayer.get_weights  s     #'<<0a	000s   )c           
         | j                   }t        |      t        |      k7  r1t        d| j                   dt        |       dt        |       d      t	        ||      D ]b  \  }}|j
                  |j
                  k7  r3t        d| j                   d|j
                   d|j
                   d      |j                  |       d y	)
z?Sets the values of `layer.weights` from a list of NumPy arrays.z,You called `set_weights(weights)` on layer 'z' with a weight list of length z, but the layer was expecting z	 weights.Layer z weight shape z. is not compatible with provided weight shape .N)r   r   rc   rS   zipr   assign)r~   r   layer_weightsr   r   s        r;   set_weightszLayer.set_weights  s    }W->tyyk J003G~ >!!$]!3 4I? 
  #=': 	#OHe~~, TYYK~hnn5E F"[[M, 
 OOE"	#rN   c                     | j                   S r>   )_dtype_policyr   s    r;   dtype_policyzLayer.dtype_policy  s    !!!rN   c                    t        j                  |      }t        | j                  t              rU| j
                  rI| j
                  | j                  v r| j                  | j
                  = || j                  | j
                  <   n|| _        |j                  7| j                  r*t        | dd      s| j                  |j                         y y y y )N_is_quantizedF)
r   r^   r   r   r   r   quantization_moder4   r   rK   )r~   r   policys      r;   r   zLayer.dtype_policy  s    ##E*d((.9diiyyD...&&tyy1,2Dtyy)!'D##/zz'$"Gf667 #Hz 0rN   c                     | j                   S )z Alias of `layer.variable_dtype`.)r   r   s    r;   rQ   zLayer.dtype  s     """rN   c                     t        | j                  t              r1| j                  r%| j                  | j                     }|j                  S | j                  }|j                  S )z5The dtype of the computations performed by the layer.)r   r   r   r   r@   r~   r  s     r;   r@   zLayer.compute_dtype  sU     d((.9dii''		2F ### ''F###rN   c                     t        | j                  t              r1| j                  r%| j                  | j                     }|j                  S | j                  }|j                  S )z.The dtype of the state (weights) of the layer.)r   r   r   r   r   r  s     r;   r   zLayer.variable_dtype  sU     d((.9dii''		2F $$$ ''F$$$rN   c                     t        | j                  t              r1| j                  r%| j                  | j                     }|j                  S | j                  }|j                  S )z=The quantization mode of this layer, `None` if not quantized.)r   r   r   r   r   r  s     r;   r   zLayer.quantization_mode  sU     d((.9dii''		2F ''' ''F'''rN   c                     | j                   S )z.The dtype layer inputs should be converted to.)r@   r   s    r;   input_dtypezLayer.input_dtype  s     !!!rN   c                     | j                   S )zBWhether this layer supports computing a mask using `compute_mask`.rx   r   s    r;   supports_maskingzLayer.supports_masking  s     %%%rN   c                     || _         y r>   r
  r   s     r;   r  zLayer.supports_masking$  s
    !&rN   c                     |S r>   r   )r~   inputsprevious_masks      r;   rw   zLayer.compute_mask(  s    rN   c                 &     | j                   |i |S r>   )compute_output_specr~   r7   r8   s      r;   symbolic_callzLayer.symbolic_call,  s    't''888rN   c           	      J     j                          d _        |}|} fd}|sMt        |      dk7  s?t        |d   d      r/t	        j
                  |d   j                         j                  k7  r8 j                  r,t        j                  ||      }t        j                  ||      } j                  sBt        j                  |      D ]*  }t        |d      rt        d| dt        |       d	       t         j                    j"                  ||      } j%                  |j&                          j)                         5   j+                  |       d d d         j-                         } j"                  D ]  }	 j/                  |	|||        t        |j0                        dk(  r|d
|j2                  v r|j4                  d
   t7        |j0                  j9                               d   }
|j0                  |
   }t        j                  t        j:                  |      }||d
<   nt        |j0                        dkD  ro|j0                  j=                         D ]R  \  }}| d}||j2                  v s|j4                  |   *t        j                  t        j:                  |      }|||<   T t        j                  t        j:                  |j&                        }	  j)                         5  t	        j>                         }d }| j@                  st	        jB                  d       }nt	        jD                   jF                        st	        jB                  d       }nq|j                   jF                  k7  rXt	        jB                   jF                        }n8 jF                   jH                  k7  rt	        jB                   jF                        }||5  tK           |i |}d d d        ntK           |i |}tO        jP                         }|8tS               }|dz  }|jU                  |      }|rtO        jV                  |      }d _,         jZ                  Pt        j                        D ]8  }t	        j\                  |      s j_                   j[                  |             : d d d         j`                  r jc                  |j&                  |       n_te        d t        j                  |      D              r:tg        jh                  d jj                   d jl                  jn                   d        jq                          ts        ||      rtu         ||       S # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w#  jq                          w xY w)NTc                 f    j                   j                  | j                  j                        S r>   )r   convert_inputrR   r  )r   r~   s    r;   maybe_convertz%Layer.__call__.<locals>.maybe_convert:  s-    $$224==$"2"2 rN   r   r   F
allow_nonezOnly input tensors may be passed as positional arguments. The following argument value should be passed as a keyword argument: z
 (of type )rZ   _maskz/outputc              3   $   K   | ]  }|d u 
 y wr>   r   ).0ms     r;   	<genexpr>z!Layer.__call__.<locals>.<genexpr>  s     HqQd]Hs   Layer 'z' (of type z) was passed an input with a mask attached to it. However, this layer does not support masking and will therefore destroy the mask information. Downstream layers will not see the mask.)	operation	call_argscall_kwargsoutputs);r   rf   r   is_backend_tensor_or_symbolicr   r   rQ   r  ry   r	   map_structurerz   flattenrc   typeCallSpecrn   rt   _assert_input_compatibility	first_argr-   _maybe_build_get_call_context_resolve_and_populate_argtensor_arguments_dictargument_namesarguments_dictr   keysget_keras_maskitemsget_autocast_scoperR   AutocastScopeis_float_dtyper@   r   rH   __call__r   distributionr   get_tensor_layoutdistribute_tensorr4   rO   	is_tensoradd_lossr  _set_mask_metadataanyr`   ra   rS   rM   rd   _maybe_reset_call_contextr   r   )r~   r7   r8   original_argsoriginal_kwargsr  r   	call_speccall_contextcontext_argarg_nameonly_tensor_argrZ   kr   expected_mask_arg_namer  current_scope	new_scoper$  r9  current_layer_pathlayoutoutputrM   s   `                       r;   r8  zLayer.__call__0  sG     " 	 4yA~0aUK((a74;K;KK&&%%mT:D''v>F 55||D) 4STJ$CCF% H$$(I;a1    $"9"94
	 	(()<)<= ""$ 	)i(	) --/22 	K**Yf	 y../14)222,,V4<	 ? ? D D FGJ"+"A"A("K))**# "&v001A5!77==? >1,-3e&)Y-E-EE //0FGO#11'2H2H!L9=56> **""I$7$7
;	-&&( 'M ' : : < 	 ,==$+$9$9$$?	$33D4F4FG %,$9$9$$?	&,,0B0BB$+$9$9$:L:L$M	''4+>+>> ' 5 5d6H6H II(" D"''"2D"CF"CD D $g.??G  0<<>+)5&&)3&);;<NOF"2"D"D#V# "
,,8"&,,w"7 M",,V4 MM$*C*CF*KLMK'MX $$''''- HDLL,GHHdii[DNN4K4K3L M4 4 **,  ?'+	 s	) 	)HD D#'M 'Mt **,sQ   U*V .C%VU7#B&V
"V,BV *U47V	<VV	V V"c                 8    | j                  | j                        r>   )_not_implemented_errorrm   r  s      r;   rm   z
Layer.call  s    ))$))44rN   c                 ,   ||j                   v r|j                   |   }n?|j                  |      |j                  |      }n|j                  j                  |d       }|j	                  ||       | j
                  j                  |d      r	||||<   y y y )NF)user_arguments_dict	get_valuer1  r^   	set_valueru   )r~   rF  rC  rD  r8   r   s         r;   r.  zLayer._resolve_and_populate_arg  s     y44411(;E##H-9 **84E ,,004@E 	x/ &&**8U;!$F8 " <rN   )return_lossesc          
         | j                          | j                  s#t        d| j                  j                   d      t        |      t        | j                        k7  rEt        d| j                  j                   dt        |       dt        | j                         d      t        |      t        | j                        k7  rEt        d| j                  j                   dt        |       dt        | j                         d      t        | j                  |      }t        | j                  |      }t        |      t        |      z   }d	}	t        j                  ||
      5 }
| j                  j                  F| j                  '  | j                  | j                   g|i ||i |}nX | j                   |i |}nE| j                  '  | j                  | j"                  g|i ||i |}n | j"                  |i |}|r| j$                  }	d	d	d	       g }| j                  D ]$  }
j'                  |      }|j)                  |       & |r||	fS |fS # 1 sw Y   IxY w)a  Call the layer without any side effects.

        Args:
            trainable_variables: List of trainable variables of the model.
            non_trainable_variables: List of non-trainable variables of the
                model.
            *args: Positional arguments to be passed to `call()`.
            return_losses: If `True`, `stateless_call()` will return the list of
                losses created during `call()` as part of its return values.
            **kwargs: Keyword arguments to be passed to `call()`.

        Returns:
            A tuple. By default, returns `(outputs, non_trainable_variables)`.
                If `return_losses = True`, then returns
                `(outputs, non_trainable_variables, losses)`.

        Note: `non_trainable_variables` include not only non-trainable weights
        such as `BatchNormalization` statistics, but also RNG seed state
        (if there are any random operations part of the layer, such as dropout),
        and `Metric` state (if there are any metrics attached to the layer).
        These are all elements of state of the layer.

        Example:

        ```python
        model = ...
        data = ...
        trainable_variables = model.trainable_variables
        non_trainable_variables = model.non_trainable_variables
        # Call the model with zero side effects
        outputs, non_trainable_variables = model.stateless_call(
            trainable_variables,
            non_trainable_variables,
            data,
        )
        # Attach the updated state to the model
        # (until you do this, the model is still in its pre-call state).
        for ref_var, value in zip(
            model.non_trainable_variables, non_trainable_variables
        ):
            ref_var.assign(value)
        ```
        zTo call stateless_call, zp must be built (i.e. its variables must have been already created). You can build it by calling it on some data.zNArgument `trainable_variables` must be a list of tensors corresponding 1:1 to z2().trainable_variables. Received list with length z, but expected z variables.zRArgument `non_trainable_variables` must be a list of tensors corresponding 1:1 to z6().non_trainable_variables. Received list with length N)state_mappingcollect_losses)r   r4   rc   rM   rd   r   r   r   r   r   r   StatelessScoper   r   r|   rematerialized_callquantized_callrm   lossesget_current_valuer   )r~   r   r   rU  r7   r8   trainable_mappingnon_trainable_mappingmappingr\  scoper$  r   new_vs                 r;   stateless_callzLayer.stateless_call  s   h 	  "zz*4>>+B+B*C D? ? 
 "#s4+C+C'DD(>>**+ ,--01D-E,F G  #D$<$< =>k	K  &'3t/K/K+LL(>>**+ ,--01H-I,J K  #D$@$@ AB+	O    8 8:MN #((*A!
 ()D1F,GG ##!-
 	%  22>##/6d66++.26<'%'G 2d114B6BG!!-N2$22499NtNvN# $$))T4V4#	%( #%-- 	2A++A.E#**51	2 3V;;///7	% 	%s   (B0I  I)c                     t        j                   j                        rt           |i |S t         j                   j                  ||      }t        |      }t         j                  || j                  j                        }  j                  di |}t        |t              r+|r)t        |d   t        t        d       f      rt!        |      }t        |t        t         t"        f      s	 t!        |      }t        |t               r7|r5t        |d   t        t        d       f      rt'        | j(                        S t+        j,                   fd|      S #  t%        d j                  j                   d|       xY w)Nr   rC  
class_namer   z)Method `compute_output_shape()` of layer zf is returning a type that cannot be interpreted as a shape. It should return a shape tuple. Received: rQ   c                 2    t        | j                        S )Nrg  )r   r@   )sr~   s    r;   r   z+Layer.compute_output_spec.<locals>.<lambda>  s    +at/A/AB rN   r   )r
   rv   compute_output_shaperH   r  r)  rn   rt   get_shapes_dict update_shapes_dict_for_target_fnrM   rd   r   r   intr(  r   dictrc   r   r@   r	   map_shape_structure)r~   r7   r8   rC  r   output_shaperM   s   `     r;   r  zLayer.compute_output_spech  sq   D5567.??? !$$d&=&=tVI *)4K:))'#>>22	K 5444C{CL <. |Ad4j0AB$\2lT5$,?@	#(#6L </ |Ad4j0AB"<t7I7IJJ ++BL !$C>>223 4% &2N	4 s   4E 'Fc                 :    | j                  | j                  d      )Nz?Should implement `def compute_output_shape(self, input_shape)`.)rP  rj  r  s      r;   rj  zLayer.compute_output_shape  s!    ))%%M
 	
rN   c                    t        j                  |      }|D ]%  }t        j                  |      rt	        d|        t        j
                         r^t        j                         }|j                  r=|D ]7  }|j                  |       | j                  j                  t        |             9 yy| j                  j                  |       y)a  Can be called inside of the `call()` method to add a scalar loss.

        Example:

        ```python
        class MyLayer(Layer):
            ...
            def call(self, x):
                self.add_loss(ops.sum(x))
                return x
        ```
        zn`add_loss()` can only be called from inside `build()` or `call()`, on a tensor input. Received invalid value: N)r	   r'  r   r<  rc   in_stateless_scopeget_stateless_scoperX  r=  rk   r   r   ri   r   )r~   lossr\  r   ra  s        r;   r=  zLayer.add_loss  s     d# 	A$$Q' LLM3P 	 %%'//1E## .ANN1%NN&&r!u-. $
 LL'rN   c                     t        j                         rRg }t        j                         }|j                  D ]+  }t	        |      | j
                  v s|j                  |       - |S | j                  d d  S r>   )r   rs  rt  r\  r   rk   r   ri   )r~   r\  ra  ru  s       r;   _get_own_losseszLayer._get_own_losses  se    %%'F//1E (d8t~~-MM$'( M<<?"rN   c                    g }| j                   D ]s  }|j                  t        j                         r.t	               s$t        j
                         j                  |      }n|}|j                  |j                  |             u |S r>   )r   r   r   rs  r   rt  r]  r   )r~   weight_regularization_lossesr   r   s       r;   _get_regularization_lossesz Layer._get_regularization_losses  s    ')$.. 
	IH##+))+4E4G //1CCHM(//0D0DQ0GH
	I ,+rN   c                    | j                   r| j                   S | j                         }| j                  d      D ]!  }|j                  |j                                # | j	                         }|j                  |       |S )zBList of scalar losses from `add_loss`, regularizers and sublayers.F)include_self)rl   rw  _flatten_layersr   rz  )r~   r\  r   ry  s       r;   r\  zLayer.losses  sz       (((%%'))u)= 	3EMM%//12	3'+'F'F'H$23rN   c                    t        j                         rdt        j                         }|j                  rD|j                  D ]5  }t        |      | j                  v s|j                  j                  |       7 | j                  j                          | j                  j                          | j                  D ]  }|j                           y r>   )r   rs  rt  rX  r\  r   rk   removeri   clearr   _clear_losses)r~   ra  r   r   s       r;   r  zLayer._clear_losses  s    %%'//1E## /A!u.++A./ 	\\ 	"E!	"rN   c                 8    | j                  | j                        r>   )rP  quantized_build)r~   rV   rE   s      r;   r  zLayer.quantized_build  s    ))$*>*>??rN   c                 8    | j                  | j                        r>   )rP  rK   )r~   rE   
type_checks      r;   rK   zLayer.quantize  s    ))$--88rN   c                    | j                   s0t        d| j                   d| j                  j                   d      t        | dd      r2t        d| j                   d| j                  j                   d|       |t        j                  vrt        d	t        j                   d
|       |dk(  r|dk(  rt        d| d      y y )Nz5Cannot quantize a layer that isn't yet built. Layer 'z' (of type 'z') is not built yet.r   Fr   z*' is already quantized with dtype_policy='z'. Received: mode=+Invalid quantization mode. Expected one of z. Received: mode=int8float16zQuantization mode='z' doesn't work well with compute_dtype='float16'. Consider loading model/layer with another dtype policy such as 'mixed_bfloat16' or 'mixed_float16' before calling `quantize()`.)	r4   rc   rS   rM   rd   r   r   r   QUANTIZATION_MODES)r~   rE   r@   s      r;   r?   zLayer._check_quantize_args  s    zz))L1H1H0I J$$ 
 4%0$)) %!!%!2!2!7!7 8 9""&) 
 ~888##1#D#D"E F""&) 
 6>my8%dV ,? ?  9>rN   c           	      X   t               }|| j                  k7  r5|3t        j                  d| d| j                   d| j                   d       | j                  dk(  r | j
                  |i |S | j                  dk(  r | j                  |i |S | j                  | j                        )NzThe RematScope at call time (z3) differs the one set during layer initialization (z0). Restoring the correct rematerialization mode z for this layer.r  float8)r   r|   r`   ra   r   
_int8_call_float8_call_quantization_mode_error)r~   r7   r8   current_remat_modes       r;   r[  zLayer.quantized_call  s    35 $"2"22".MM/0B/C D$$% &@##$$4	6 !!V+"4??D3F33##x/$4$$d5f55//0F0FGGrN   c                 8    | j                  | j                        r>   )rP  r  r  s      r;   r  zLayer._int8_call%  s    ))$//::rN   c                 8    | j                  | j                        r>   )rP  r  r  s      r;   r  zLayer._float8_call(  s    ))$*;*;<<rN   c           
          t        |      r|j                  }d}nt        |      }d}|d|z   nd}t        d| j                  j                   d| d| d|       S )	Nmethod	attribute  r   z does not have a `z` z implemented.)callablerd   strNotImplementedErrorrM   )r~   attrr   	attr_name	attr_types        r;   rP  zLayer._not_implemented_error+  sl    D>I ID	I#I?cCi"T^^,,--?	{"kse-
 	
rN   c                 @    t        dt        j                   d|       S )Nr  z. Received: quantization_mode=)r  r   r  )r~   rE   s     r;   r  zLayer._quantization_mode_error8  s-    "9001 2++/&2
 	
rN   c                 h    | j                   | j                  z   }t        |      D ]  \  }}||| <    y)a   Saves the state of the layer.

        You can override this method to take full control of how the state of
        the layer is saved upon calling `model.save()`.

        Args:
            store: Dict where the state of the model will be saved.
        N)r   r   	enumerate)r~   storeall_varsir   s        r;   save_own_variableszLayer.save_own_variables?  s=     ,,t/L/LLh' 	DAqEQCM	rN   c                 Z   | j                   | j                  z   }t        |j                               t        |      k7  rt        |      dk(  rY| j                  sMt        d| j                   dt        |j                                d| j                   d| j                   d	      t        d| j                   dt        |       dt        |j                                d	|D cg c]  }|j                   c}       t        |      D ]  \  }}|j                  ||            y
c c}w )a  Loads the state of the layer.

        You can override this method to take full control of how the state of
        the layer is loaded upon calling `keras.models.load_model()`.

        Args:
            store: Dict from which the state of the model will be loaded.
        r   r   zY' was never built and thus it doesn't have any variables. However the weights file lists z variables for this layer.
In most cases, this error indicates that either:

1. The layer is owned by a parent layer that implements a `build()` method, but calling the parent's `build()` method did NOT create the state of the child layer 'z'. A `build()` method must create ALL state for the layer, including the state of any children layers.

2. You need to implement the `def build_from_config(self, config)` method on layer 'a-  ', to specify how to rebuild it during loading. In this case, you might also want to implement the method that generates the build config at saving time, `def get_build_config(self)`. The method `build_from_config()` is meant to create the state of the layer (i.e. its variables) upon deserialization.z' expected z variables, but received z% variables during loading. Expected: N)	r   r   r   r2  r4   rc   rS   r  r   )r~   r  r  r   r  s        r;   load_own_variableszLayer.load_own_variablesL  s(    ,,t/L/LLuzz|H-8}!$** dii[ )669%**,6G5H I( )-		{ 3!
 "& ,NN . $))KH ? uzz|$% &.67aff78:  h' 	$DAqHHUaS]#	$ 8s   #D(c                     |j                   r| j                  j                  d|       n| j                  j                  d|       | j                   sd|_         | j                  |       y )Nr   r   F)rP   rA   add_to_store_post_track_variable)r~   r   s     r;   r   zLayer._track_variablex  sR    MM&&'<hGMM&&'@(K~~!&H!!(+rN   c                     | j                   j                  }| j                   j                          | j                   j                  |       |du r| j                   j	                          | j                  |       y r,   )rA   r   rB   untrackrD   _post_untrack_variable)r~   r   previous_lock_states      r;   _untrack_variablezLayer._untrack_variable  sZ    "mm22h'$&MM ##H-rN   c                     t        d      )NzLayer `add_metric()` method is deprecated. Add your metric in `Model.compile(metrics=[...])`, or create metric trackers in init() or build() when subclassing the layer or model, then call `metric.update_state()` whenever necessary.)r  r  s      r;   
add_metriczLayer.add_metric  s    !:
 	
rN   c                     | j                   st        d| j                   d      t        j                  | j
                        S )zqCount the total number of scalars composing the weights.

        Returns:
            An integer count.
        z+You tried to call `count_params` on layer 'zX', but the layer isn't built. You can build it manually via: `layer.build(input_shape)`.)r4   rc   rS   r   count_paramsr   r   s    r;   r  zLayer.count_params  sG     zz!YYK (./  ))$,,77rN   c                    | j                   ry t        |      }t        t        |j	                               d       }t        j                  | j                        s[t        | j                  ||| j                  j                        } | j                  di | | j                  |j                         y t        |       r+	 t        j                  | j                   fi |j"                   | j                  |       y # t$        $ rA}|j&                  rY d }~y t)        j*                  d| j,                   d| d       Y d }~Wd }~ww xY w)Nre  r   aK  ' looks like it has unbuilt state, but Keras is not able to trace the layer `call()` in order to build it automatically. Possible causes:
1. The `call()` method of your layer may be crashing. Try to `__call__()` the layer eagerly on some test input first to see if it works. E.g. `x = np.random.random((3, 4)); y = layer(x)`
2. If the `call()` method is correct, then you may need to implement the `def build(self, input_shape)` method on your layer. It should create all variables used by the layer (e.g. by calling `layer.build()` on all its children layers).
Exception encountered: ''z''r   )r4   rk  nextiterrp   r
   rv   rJ   rl  rM   rd   r*  r+  r   r   r  rm   r1  rC   eagerr`   ra   rS   )r~   rC  r   first_shapees        r;   r,  zLayer._maybe_build  s   ::%i04 2 2 45t< 

+:

'#>>22	K DJJ%% ,,Y-@-@A $D)++II!*!9!9, 	

;'  ??dii[ )0 12s"6 	s   *C> >	EE&EEc                     t        j                  d |      }	 t        j                  | j                  |       y#  Y yxY w)Nc                 ,    t        j                  |       S r>   )r   r   ri  s    r;   r   z8Layer._build_by_run_for_single_pos_arg.<locals>.<lambda>  s    g))!, rN   TF)r	   ro  r   r  rm   )r~   rV   input_tensorss      r;    _build_by_run_for_single_pos_argz&Layer._build_by_run_for_single_pos_arg  s=    00,k
	''		=A	s    : >c                 6   t        d |j                         D              rl|j                         D ci c]/  \  }}t        j                  |d      t        j                  |      1 }}}	 t        j                  | j                  fi | yyc c}}w #  Y yxY w)Nc              3   2   K   | ]  }t        |        y wr>   )is_shape_tuple)r  ri  s     r;   r  z1Layer._build_by_run_for_kwargs.<locals>.<genexpr>  s     ?Q~a ?   _shapeTF)	allrp   r4  r
   removesuffixr   r   r  rm   )r~   r   rH  r   r  s        r;   _build_by_run_for_kwargszLayer._build_by_run_for_kwargs  s    ?+*<*<*>??
 !, 1 1 3 Au ""1h/1D1DU1KKM 
++DIIGG
 s   4B, B Bc                 j    d| j                   j                   d| j                   d| j                   dS )N<z name=z, built=>)rM   rd   rS   r4   r   s    r;   __repr__zLayer.__repr__  s2    ''(tyyk$**QO	
rN   c                 "    | j                         S r>   )r  r   s    r;   __str__zLayer.__str__  s    }}rN   c                     | j                  ||      \  }}|dk7  r7t        | d      s| j                          | j                  j	                  |      }t
        |   ||      S )NrA   )_setattr_hookr   r}   rA   trackrH   __setattr__)r~   rS   r   rM   s      r;   r  zLayer.__setattr__  s]    ((u5e:4,((*MM''.Ew"4//rN   c                     t        | |      }t        |t        j                        r5dd l}| j                  |       t        |   |       |j                          y t        |   |       y )Nr   )	r   r   r   r   gcr  rH   __delattr__collect)r~   rS   r9   r  rM   s       r;   r  zLayer.__delattr__  sU    dD!c7++,
 ""3'G%JJLG%rN   c                 d    t        | dd      r#t        d| j                  j                   d      y )Nr]   Tz
In layer 'zh', you forgot to call `super().__init__()` as the first statement in the `__init__()` method. Go add it!)r   RuntimeErrorrM   rd   r   s    r;   r   zLayer._check_super_called  s<    4$'T^^445 69 9  (rN   c                     | j                   r.	 t        j                  | j                   || j                         y y # t        $ r t	        j                         dk(  rY y  w xY w)N)
layer_namer"   )r   assert_input_compatibilityrS   SystemErrorr   )r~   arg_0s     r;   r*  z!Layer._assert_input_compatibility  sX    ??55OOUtyy 
  ??$/
 s   ,<  A A c                     t        j                  d      }|2t        |       }t        j                  d|       | j	                          |S )z'Returns currently active `CallContext`.current_call_ctxentry_layer)r   get_global_attributeCallContextset_global_attributer  r~   layer_call_ctxs     r;   r-  zLayer._get_call_context'  sJ    %::;MN!(T:N--"N  rN   c                 ~    t        j                  d      }||j                  | k(  rt        j                  dd        y y )Nr  )r   r  r  r  r  s     r;   r@  zLayer._maybe_reset_call_context3  s=    %::;MN!^%?%?4%G--.@$G &HrN   c                 X   g }|r|j                  |        t               }t        j                  | j                        }|ri|j                         }t        |      |v r |j                  t        |             |j                  |       |r|j                  |j                         |ri|S r>   )	r   rj   collectionsdequer   popleftr   r   
extendleft)r~   r|  	recursiver   seen_object_idsr  r   s          r;   r}  zLayer._flatten_layers8  s    MM$%!!$,,/MMOE%yO+5	*MM%   /  rN   c                    t        j                  |      }t        d |D              }|ry | j                  ||      }|y t        j                  |      }t	        ||      D ]a  \  }}	t        j                  |      |	t        j
                         dk(  rt        j                  d       Lt        j                  ||	       c y )Nc              3   J   K   | ]  }t        j                  |      d u  y wr>   )r   r3  r  r   s     r;   r  z+Layer._set_mask_metadata.<locals>.<genexpr>L  s%      $
67G""1%T1$
s   !#r$   zNThe NumPy backend does not support masking at thistime. Masks will be ignored.)
r	   r'  r  rw   r   r   r3  r`   ra   set_keras_mask)
r~   r  r$  r  flat_outputsmask_already_computedoutput_masks
flat_maskstensorrZ   s
             r;   r>  zLayer._set_mask_metadataI  s    ||G, # $
;G$
 !
 !((?\\,/
j9 	9LFD%%f-5$:J??$/MM7
 **648	9rN   c                     | j                          t        | 	         }| j                  t	        j
                  | j                        d}| j                  "t        j
                  | j                        |d<   i ||S )N)rP   rQ   rO   )	r   rH   
get_configrP   r   	serializer   rO   r   )r~   base_configr   rM   s      r;   r  zLayer.get_configa  s}      "g(*#--d.?.?@
 $$0-9-C-C)).F)* )+(((rN   c                 z    | j                   t               | _         t        j                  | j                  |       S )Nr   )r{   r   r   r   rS   r   s    r;   r-   zLayer._open_name_scopeo  s/    $ ,D!!$))D99rN   c                     d } j                   j                  dk(  rt        j                        S  j                   j                  dk(  r7 j                   j                   j                  v rt        j                        S  j                   j                  dk(  rv  j
                  |i |}t        t        j                  t        j                  ||                  }|r.| j                   j                  kD  rt        j                        S S  j                   j                  dk(  r>t         d      xr  j                  du}|r t        j                         fd       }|S S )	zEnable rematerialization dynamically for layer's call method.

        Args:
            layer_call: The original `call` method of a layer.

        Returns:
            Rematerialized layer's `call` method.
        c                     t        | t              r1t        j                  | j                  D cg c]  }|xs d
 c}      S dS c c}w )Nr   r   )r   r   mathprodr   )r   ds     r;   compute_sizez/Layer.rematerialized_call.<locals>.compute_size~  sC     a- 		1773a16634 3s   Afulllist_of_layerslarger_thanactivations
activationNc                      j                   }t        j                  |      _         	  | i ||_         S # |_         w xY wr>   )r  r   )r7   r8   original_activation
layer_callr~   s      r;   &rematerialized_activation_call_wrapperzILayer.rematerialized_call.<locals>.rematerialized_activation_call_wrapper  s@    *.//'&+kk2E&FDO>)4:6:*=*=s	   8 	A)r|   rE   r   rS   layer_namesr  sumr	   r'  r&  output_size_thresholdr   r  	functoolsr   )	r~   r  r7   r8   r  output_specoutput_sizehas_activationr  s	   ``       r;   rZ  zLayer.rematerialized_callt  sL   	   F*;;z** ""&66II))555;;z** ""m32$22DCFCKT//kJKK $"2"2"H"HH{{:.." ! ""m3l+Kt0K  ,> -> >=rN   c                     | j                   rt        d      | j                  t        |      z  | _        | j                  j                  |D ci c]  }||| j                  v  c}       yc c}w )uD  Registers call-context args for this layer.

        If this layer declares a `call()` method that accepts
        one or more of the given args, those args will be
        automatically injected into the call signature of this
        layer. This layer will also propagate the args to any
        nested sublayers that are called from within this layer.

        If this layer doesn't declare a `call()` method that
        accepts one or more of the given args, these args will
        simply be propagated to any nested sublayers without
        being injected into the call signature of this layer.
        This is useful for propagating custom arguments
        from top-level layers/models to sublayers.

        Example:
        ```
        class Inner(layers.Layer):

            def __init__(self):
                super().__init__()
                # Register `foo_mode` as a call-context arg
                self._register_call_context_args("foo_mode")

            def call(self, x, foo_mode=False):
                # If foo_mode=True add 1, otherwise add 0
                add_val = ops.where(foo_mode, 1.0, 0.0)
                return x + add_val

        class Outer(layers.Layer):
            def __init__(self):
                super().__init__()
                self.inner = Inner()

            def call(self, x):
                # We don't explicitly pass foo_mode here—Base Layer.__call__
                # should inject it into `self.inner`
                return self.inner(x)

        sample_input = np.array([[1.0], [2.0]])

        # Sequential model
        seq = models.Sequential([Outer()])

        # Tell the Sequential model to propagate foo_mode down
        # the call-stack
        seq.register_call_context_args("foo_mode")

        # foo_mode=True -> input + 1
        out_true = seq(sample_input, foo_mode=True)
        z=Cannot add call-context args after the layer has been called.N)rf   r  rt   rj   ru   updaterq   )r~   namesr   s      r;   _register_call_context_argsz!Layer._register_call_context_args  sj    h <<O  #'"9"9CJ"F""))EJKcS3$8888K	
Ks   A,)NTTNNN)
NNNTTNNnoneFN)Tr>   )TT)Yrd   
__module____qualname____doc__rI   r\   r    no_automatic_dependency_trackingr}   r   propertyr   r   setterr
   defaultrJ   r6   r   r   r   r   r   rP   r   r   r   r   r   r   r   r   r   r   r   rQ   r@   r   r   r  r  rw   r  r   filter_tracebackr8  rm   r.  rc  r  rj  r=  rw  rz  r\  r  r  rK   r?   r[  r  r  rP  r  r  r  r   r  r  r  r,  r  r  r  r  r  r  r   r*  r-  r@  r}  r>  r   r  r-   rZ  r  __classcell__)rM   s   @r;   r)   r)   E   s   Tl'X "J#X ..0A /0Ad
       ! ! ]] @04" 
8  %KZ   $ $&  4 : : > >  ( 8 8 	< 	<    1#$ " " 
8 
8 # # $ $ % % ( ( " " & & ' ' ]] 9 %%i &iV5%. %% o0 &o0b,\ ]]
 
(<	#, 	 	
"@96H*;=

*$X,.
8 . `	$

0& 
H
"90 ) ):
6p<
rN   r)   c                 n    |r| yt        j                  |       xs t        | t         j                        S r,   )r   r<  r   r   )r   r  s     r;   r%  r%    s/    aiQE:a1D1D#EErN   c                       e Zd Zd Zy)r)  c                    |D ci c]&  }||v r ||j                   vr||j                  |      ( }} |j                  |i |}i ||j                  | _        |j                          i }g }	i }
g }g }g }|j                  j                         D ]  \  }}|||<   |	j                  |       t        |      r(|j                  |       |j                  |       ||
|<   Ot        j                  |      set        |      dkD  stt        j                  |      }t        d |D              r9|j                  |       |j                  |       ||
|<   |j                  |       t        d |D              st        d| d|        || _        |	| _        |
| _        || _        || _        ||	d      | _        t        d | j$                  j-                         D              rd| _        y d| _        y c c}w )	Nr   c              3   6   K   | ]  }t        |d         yw)Tr  Nr%  r  s     r;   r  z$CallSpec.__init__.<locals>.<genexpr>  s"       2!EE   c              3   2   K   | ]  }t        |        y wr>   r"  r  s     r;   r  z$CallSpec.__init__.<locals>.<genexpr>  s     Oa6q9Or  zfIn a nested call() argument, you cannot mix tensors and non-tensors. Received invalid mixed argument: =c              3   F   K   | ]  }t        j                  |        y wr>   )r   r<  r  s     r;   r  z$CallSpec.__init__.<locals>.<genexpr>&  s       
%&Ga 
s   !TF)ro   r_   r1   r2   rR  apply_defaultsr4  r   r%  r	   	is_nestedr   r'  r  r?  rc   r1  r0  r/  tensor_arguments_namesnested_tensor_argument_namesr+  rp   r  )r~   r0   call_context_argsr7   r8   rE  r"  
bound_argsarg_dict	arg_namestensor_arg_dicttensor_argstensor_arg_namesnested_tensor_arg_namesrS   r   flat_valuess                    r;   r\   zCallSpec.__init__  s     1
f$I<P<P)P K00
	 
 $Y^^T4V4
 $Ii#H:3G3G#H !!#	"$%//557 	KD%"HTNT",U3""5) ''-(-%&3u:>"ll51 (   &&u-$++D1,1OD)+2248O;OO$<  &%* %	0 ''%4"&6#,C)!)A,/ 
*.*D*D*K*K*M
 
 DJDJk
s   +G2N)rd   r  r  r\   r   rN   r;   r)  r)    s    9rN   r)  c                     t        j                  |       } |j                  |i |}i }|j                  j	                         D ]
  \  }}|||<    |S )z5Return a dict mapping argument names to their values.)r/   r0   r1   r2   r4  )fnr7   r8   sigr,  r-  rS   r   s           r;   get_arguments_dictr7  .  s\    


B
C4*6*JH!++113 eOrN   c                 4   i }| j                   j                         D ]x  \  }}|dk(  s|j                  d      r|dk(  s|dk(  r(|| j                  v rt	        j
                  d |      || d<   Tt        j                  |j                        || d<   z |S )zConvert the call() arguments dict into a dict of input shape arguments.

    Example:

    ```
    >>> get_shapes_dict(call_spec)
    {"input_a_shape": (2, 3)}
    ```
    rZ   r  r8   r7   c                 @    t        j                  | j                        S r>   )r   standardize_shaper   r   s    r;   r   z!get_shapes_dict.<locals>.<lambda>L  s    '33AGG< rN   r  )	r/  r4  endswithr*  r	   r&  r   r:  r   )rC  r   rH  r   s       r;   rk  rk  8  s     K//557 K1;!**W-=AK	666(,(:(:<a)K1#V% )0(A(A!''(JK1#V%K rN   c                    t        j                  |       ryt        j                  |       }g }|j                  j                         D ]F  \  }}|j                  |j                  |j                  |j                  fv s6|j                  |       H t        |      dk(  r/|d   }t        |j                               }	|	r	|	d   }
||
iS d}
||
iS i }|D ]  }| j                  }d| d| d}|j                  d      st!        | d| d	| d
| d      t        j"                  |d      }||j$                  vrt!        | d| d| d
| d| d
      ||v s||   ||<    |S )a  Updates a `shapes_dict` for `build()` or `compute_output_shape()`.

    This function will align a dictionary of the shapes of all tensor
    passed to `call`, with the signatures of `build()` or
    `compute_output_shape()`.

    The alignment is a follows:

    - If `build()` or `compute_output_shape()` accept only one argument,
        forward the shape of the first positional argument from call without
        checking any argument names.
    - If `build()` or `compute_output_shape()` accept multiple arguments,
        enforce that all argument names match a call argument name, e.g.
        `foo_shape` would match call argument `foo`.

    Returns:
        An updated `shapes_dict` that can be used to invoke
        `target_fn(**shapes_dict)`.
    Nr   r   zFor a `z()` method with more than one argument, all arguments should have a `_shape` suffix and match an argument from `call()`. E.g. `z(self, foo_shape, bar_shape)` r  z For layer 'z', Received `z()` argument `z"`, which does not end in `_shape`.z', received `z(`, but `call()` does not have argument `z`.)r
   rv   r/   r0   ro   r4  kindPOSITIONAL_OR_KEYWORDPOSITIONAL_ONLYKEYWORD_ONLYr   r   r   rp   rd   r;  rc   r  r1  )	target_fnr   rC  rf  r6  expected_namesrS   paramkeyrp   rV   r8   method_nameerror_preambleexpected_call_args                  r;   rl  rl  S  s   2 	"


I
&CN~~++- (e::''!!
 

 !!$'( >aQ{))+, )K [!! K[!! F -((k] #$$/=0NP 	
 }}X&!",zl ;(M *6;= 
 "..tX>I$<$<<!",zl ;(M *6 %&b*  ;&t,F4L--0 MrN   c                        e Zd Zd ZddZd Zy)r  c                     || _         y r>   r  )r~   r  s     r;   r\   zCallContext.__init__  s
    &rN   Nc                     t        | ||      S )z<Get the context value for `arg_name`, or `default` if unset.)r   )r~   rF  r  s      r;   rS  zCallContext.get_value  s    tXw//rN   c                     t        | ||       y)z0Set `arg_name` = `value` on this context object.N)setattr)r~   rF  r   s      r;   rT  zCallContext.set_value  s    h&rN   r>   )rd   r  r  r\   rS  rT  r   rN   r;   r  r    s    '0'rN   r  c                 V    t        | t        t        f      xr t        d | D              S )Nc              3   H   K   | ]  }|d u xs t        |t                y wr>   )r   rm  )r  r  s     r;   r  z!is_shape_tuple.<locals>.<genexpr>  s(      0,-T	'Z3''0s    ")r   r   r   r  r  s    r;   r  r    s.    a$' C 0120 - rN   c                 :    t        d | j                  D              S )Nc              3   6   K   | ]  }|j                      y wr>   )r4   )r  lrs     r;   r  z+might_have_unbuilt_state.<locals>.<genexpr>  s     4288|4r#  )r?  r   )r   s    r;   r   r     s    4emm444rN   )F)Fr  r  r  r/   r  r`   r   	keras.srcr   r   r   r   r   r	   r
   keras.src.api_exportr   keras.src.backendr   keras.src.backend.commonr   r   %keras.src.backend.common.keras_tensorr   #keras.src.backend.common.name_scoper   keras.src.backend.common.rematr   'keras.src.backend.common.symbolic_scoper   keras.src.distributionr   keras.src.dtype_policiesr   keras.src.layersr   keras.src.metrics.metricr   keras.src.ops.noder   keras.src.ops.operationr   keras.src.saving.keras_saveabler   keras.src.utilsr   r   r   r   "keras.src.backend.tensorflow.layerr   r[   keras.src.backend.jax.layerr!   keras.src.backend.torch.layerr#   keras.src.backend.numpy.layerr%    keras.src.backend.openvino.layerr'   r  r)   r%  r)  r7  rk  rl  r  r  r   r   rN   r;   <module>rg     sX  $        ! $ " "   - ) 1 * F < A E 3 3 ' + # - 9 ( ) + $7??$JW__%DW__'!HW__'!HW__*$N

OGOO%&&KL 
 }234b
L)] b
 5b
J5F: :z6IX
' 
'5rN   