
    BVhh                        d 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"  ejF                         Z$d;dZ%de
jL                  dddddddejN                  jP                  ejR                  jT                  dfdZ+d Z,d Z-d Z.d Z/dZ0d Z1d Z2d<d Z3d! Z4d" Z5d# Z6d$ Z7d% Z8d& Z9 G d' d(e:      Z; G d) d*e:      Z<d+ Z=d, Z>d- Z?d=d.Z@d/ ZAdaBd0 ZCd1 ZDd2 ZE G d3 d4e:      ZF G d5 d6eF      ZGd7 ZHd8 ZId9 ZJd:ZKy)>z?Contains private utilities used mainly by the base Layer class.    N)tf2)distribute_lib)context)dtypes)ops)sparse_tensor)tensor_shape)tensor_util)backend)control_flow_util)
tf_inspect)tf_utils)	array_ops)variable_v1)	variables)ragged_tensor)base)saveable_object_util)nestc                 \    ddl m} |j                  || j                        }| ||       fS )Nr   )metrics)namedtype)tensorflow.python.kerasr   Meanr   )valuer   metrics_module
metric_objs       _/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/keras/engine/base_layer_utils.pycreate_mean_metricr    +   s1     @""EKK"@*	Z&	&&    Tc                 8   d}|t        |      sd}|r|}d}n@t        j                  |      r |       }t        j                  |||      }|j
                  }|d}t        j                  |      }t        j                  || |||||||	|
||r|      S d      S )a9  Temporary util to create a variable (relies on `variable_scope.variable`).

  Some reuse-related technicalities prevent us from using
  `variable_scope.get_variable()` directly, so we use a subcomponent
  that has fewer constraints (`variable_scope.variable()`).

  In the longer term, it seems like a similar "default variable creator" method
  should exist in `Trackable` instead. When this happens, we can get
  rid of this temporary solution.

  TODO(fchollet): remove this method when no longer needed.

  Args:
    name: Variable name.
    shape: Variable shape.
    dtype: The type of the variable. Defaults to `self.dtype` or `float32`.
    initializer: Initializer instance (callable).
    trainable: Whether the variable should be part of the layer's
      "trainable_variables" (e.g. variables, biases)
      or "non_trainable_variables" (e.g. BatchNorm mean, stddev).
      Note, if the current variable scope is marked as non-trainable
      then this parameter is ignored and any added variables are also
      marked as non-trainable. `trainable` defaults to `True` unless
      `synchronization` is set to `ON_READ`.
    caching_device: Passed to `tf.Variable`.
    validate_shape: Passed to `tf.Variable`.
    constraint: Constraint instance (callable).
    use_resource: Whether to use a `ResourceVariable`.
    collections: List of graph collections keys. The new variable is added to
      these collections. Defaults to `[GraphKeys.GLOBAL_VARIABLES]`.
    synchronization: Indicates when a distributed a variable will be
      aggregated. Accepted values are constants defined in the class
      `tf.VariableSynchronization`. By default the synchronization is set to
      `AUTO` and the current `DistributionStrategy` chooses
      when to synchronize. If `synchronization` is set to `ON_READ`,
      `trainable` must not be set to `True`.
    aggregation: Indicates how a distributed variable will be aggregated.
      Accepted values are constants defined in the class
      `tf.VariableAggregation`.
    partitioner: Not handled at this time.

  Returns:
    Variable instance.
  FNT)r   )initial_valuer   	trainablecaching_devicer   validate_shape
constraintuse_resourcecollectionssynchronizationaggregationshape)
callabler   isclass	functoolspartial
base_dtyper	   TensorShaper   
VariableV1)r   r,   r   initializerr$   r%   r&   r'   r(   r)   r*   r+   partitionerinitializing_from_valueinit_valvariable_dtypevariable_shapes                    r   make_variabler:   3   s    r "Xk%:"HN +&Mk  e5AH%%NL  ++E2.			##%,N
8 8 37
8 8r!   c                 4    d }t        j                  ||       S )zRetrieves the output mask(s) of the previous node.

  Args:
      input_tensors: An arbitrary structure of Tensors.

  Returns:
      A mask tensor or list of mask tensors.
  c                     t        | dd       S )N_keras_maskgetattr)xs    r   _collect_previous_maskz5collect_previous_mask.<locals>._collect_previous_mask   s    1mT**r!   r   map_structure)input_tensorsrA   s     r   collect_previous_maskrE      s    + 
		2M	BBr!   c                 L    t        d t        j                  |       D              S )Nc              3   4   K   | ]  }t        |d         yw_keras_historyNhasattr.0r@   s     r   	<genexpr>z*have_all_keras_metadata.<locals>.<genexpr>   s     IaWQ()Is   )allr   flatten)tensorss    r   have_all_keras_metadatarR      s    	I4<<3HI	IIr!   c                 T    t        j                  | t        j                               S )N)r,   r   )r   placeholderr   floatx)r,   s    r    generate_placeholders_from_shaperV      s    			U'..2B	CCr!   c                 6    t        | t               g       \  }}|S )a  Wraps TensorFlow Operations for compatibility with the Functional API.

  This method checks to see if a Tensor in `tensors` is missing Keras metadata
  and has its origin in a Keras `Input` Layer. If so, this method will replace
  the raw TensorFlow Operations that created this tensor with
  `TensorFlowOpLayer` instances that create identical operations.

  Any Tensors not originating from a Keras `Input` Layer will be treated as
  constants when constructing `TensorFlowOpLayer` instances.

  Args:
    tensors: A structure of Tensors, some of which come from raw TensorFlow
      operations and need to have Keras metadata assigned to them.

  Returns:
    created_layers: List. The `TensorFlowOpLayer` instances created to wrap
      the raw Tensorflow operations.
  )_create_keras_history_helperset)rQ   _created_layerss      r   create_keras_historyr\      s    & 37CE2F!^	r!   Fc           	      0   t        j                         rt        d      ddlm} t        j                  |       }g }g }|D ]  }t        |dd      t        |t        j                  t        j                  f      r|j                  |j                         Xt        j                  |      r|j                  |       |j                  }||vst!        |j"                        }	i }
g }t%        |	      D ]  \  }}t'        |      r|j                  |       #t)        j*                         xr t        j                          }t-        j.                  t        j0                               }|s|st2        r||
|<   t        j4                         5   t7        j8                  g |      g       |
|<   ddd        t;        |      }t=        |||      \  }}|j>                  }|j@                  jC                         }|jE                  ||
|      }|j                  |       |jG                  |fi |jH                         |jK                  |g        |s|r1d}t        d	jM                  |tO        |      tO        |      
            ||fS # 1 sw Y   xY w)a  Helper method for `create_keras_history`.

  Args:
    tensors: A structure of Tensors for which to create Keras metadata.
    processed_ops: Set. TensorFlow operations that have already been wrapped in
      `TensorFlowOpLayer` instances.
    created_layers: List. The `TensorFlowOpLayer` instances created.

  Returns:
    Tuple. First element is the updated set of TensorFlow Operations that
    have been wrapped in `TensorFlowOpLayer` instances. Second element is
    a list of the `TensorFlowOpLayer` instances created.
  zB`create_keras_history` should only be called if eager is disabled!r   )
base_layerrI   N)	constantsr   )argskwargsoutputsz
    weights_mult = lambda x: tf.sparse.sparse_dense_matmul(x, weights)
    output = tf.keras.layers.Lambda(weights_mult)(input)
    a  Tensorflow ops that generate ragged or sparse tensor outputs are currently not supported by Keras automatic op wrapping. Please wrap these ops in a Lambda layer: 

```
{example}
```
Sparse ops encountered: {sparse_ops}
Ragged tensors encountered: {ragged_tensors}
)example
sparse_opsragged_tensors)(r   #executing_eagerly_outside_functions
ValueErrortensorflow.python.keras.enginer^   r   rP   r?   
isinstancer   SparseTensorSparseTensorValueappendopr   	is_raggedlistinputs	enumerateuses_keras_historyr   in_cross_replica_contextr   GraphOrParentsInXlaContextget_default_graph_UNSAFE_GRAPH_OP_LAYER_CREATION
init_scoper   functionunnest_if_single_tensorrX   r   node_defSerializeToStringTensorFlowOpLayer_set_connectivity_metadatarb   updateformatstr)rQ   processed_opsr[   r^   tensor_listrd   re   tensorrm   	op_inputsr_   layer_inputsiop_inputds_with_session	using_xlar   rz   op_layerlambda_examples                       r   rX   rX      s    	,,.
LN N
 8W%+*. 0!fv'.:++]-L-LMO		"&!F#	B	ryy/iil"9- @+!Xh'


h
'
 557 <99;;  (BB##%')	-L $IaL! @;W--b(;B?il@ @#@& -\:l&B
~'7#m^WWd..0h--
id . 4hH%))** *  B4 a0!b >N 	9
 :@":~. :@ :0	1 	1 
	&&=@ @s    JJc                 v    t        j                  |       }t        | t              st	        |      dk(  r|d   } | S )N   r   )r   rP   ri   dictlen)rD   flat_input_tensorss     r   ry   ry   #  s:    ||M2 
M4	(S1C-D-I&q)M	r!   c                     t        j                  |       }t               j                  r|syt	        d |D              ryt        |       S )a  Check if any Tensors need to be wrapped in TensorFlowOpLayers.

  This will never return True inside a sublayer, because sublayers
  do not need to create Keras History. Otherwise, this returns True
  if one or more of `tensors` originates from a `keras.Input` and
  does not have `_keras_history` set.

  Args:
    tensors: An arbitrary nested structure of Tensors.
    ignore_call_context: Whether to ignore the check of if currently
      outside of a `call` context. This is `True` when creating
      KerasHistory inside `Node`, where we always know that Tensors
      are being used with the Functional API.

  Returns:
    Bool, whether at least one Tensor needs to be wrapped.
  Fc              3   :   K   | ]  }t        |d d      du  ywrH   r>   )rM   r   s     r   rN   z&needs_keras_history.<locals>.<genexpr>C  s'      	#
 f&-T9	#s   )r   rP   call_contextin_callrO   rr   )rQ   ignore_call_contextrD   s      r   needs_keras_historyr   .  sH    $ ,,w'-^$7 	#!	# # 	G	$$r!   c                  *    t               j                  S )z7Returns if currently executing inside of a Keras graph.)r   in_keras_graph r!   r   is_in_keras_graphr   K  s    		&	&&r!   c                  B    t        j                         xs
 t               S )z4Returns if in eager mode or inside of a tf.function.)r   executing_eagerlyis_in_tf_functionr   r!   r   is_in_eager_or_tf_functionr   P  s    		"	"	$	;(9(;;r!   c                      t        j                         syt        j                         syt               ryt        j                         } t        | dd      r| j                  j                  d      ryy)z#Returns if inside of a tf.function.Fr   wrapped_functionT)r   rf   inside_functionr   ru   r?   r   
startswith)graphs    r   r   r   U  s^     
	0	0	2				



!%eVU#jj./	r!   c                 l   t               }t        j                  |       }|ryg }|D ]m  }t        |      |v r|j	                  t        |             t        |dd      9t        |dd       y	 |j                  |j                  j                         o |}|ryt        |        y# t        $ r Y w xY w)a  Check if at least one Tensor originates from a `keras.Input`.

  This is `True` if at least one Tensor has its origin in a `keras.Input`.
  Any Tensor that originates from a `keras.Input` will have a dependency
  Tensor with a `_keras_history` attribute attached. Tensors that have
  already been checked to not originate from a `keras.Input`
  are marked as `_keras_history_checked`.

  Args:
    tensors: An arbitrary nested structure of Tensors.

  Returns:
    Bool, whether at least one Tensor originates from a `keras.Input`.
  _keras_history_checkedNrI   TF)rY   r   rP   idaddr?   extendrm   rp   AttributeErrormark_checked)rQ   checked_tensorstensors_to_checknew_tensors_to_checkr   s        r   rr   rr   g  s     E/\\'*" 	F	&"V*%	14	8	D	)4	0	<##FII$4$45" ,' 	. w	  s   /%B''	B32B3c                 6    d }t        j                  ||        y)zMarks that these Tensors should not be tracked.

  This prevents Layers from attempting to create TensorFlowOpLayers
  for these Tensors.

  Args:
    tensors: An arbitrary structure of Tensors.
  c                     d| _         y )NT)r   )r   s    r   _mark_checkedz#mark_checked.<locals>._mark_checked  s
    $(F!r!   NrB   )rQ   r   s     r   r   r     s    ) ]G,r!   c                  V    t        t        dd      } | t               } | t        _        | S )z'Returns currently active `CallContext`.r   N)r?   _call_contextCallContextr   )call_ctxs    r   r   r     s*    ]ND9(}H!)M	/r!   c                       e Zd ZdZd ZddZed        Zed        Zed        Z	ed        Z
ed	        Zed
        Zed        Zy)r   a]  Keeps track of properties currently inside a Layer/Model's `call`.

  Attributes:
    in_call: Whether currently inside the `call` of a Layer.
    layer: The `Layer` whose `call` is currently active.
    inputs: The inputs to the currently active `Layer`.
    build_graph: Whether currently inside a Graph or FuncGraph.
    training: Whether currently executing in training or inference mode.
    saving: Whether currently saving to SavedModel.
    frozen: Whether currently executing inside a `Layer` with `trainable` set to
      `False`.
    in_keras_graph: Whether executing inside the Keras Graph.
  c                 :    d| _         d d dd d d| _        d| _        y )NFlayerrp   build_graphtrainingsaving)r   _state_in_keras_graphselfs    r   __init__zCallContext.__init__  s-     DLDK !Dr!   Nc                 *    |||||d}t        | |      S )a  Push a Layer and its inputs and state onto the current call context.

    Args:
      layer: The `Layer` whose `call` is currently active.
      inputs: The inputs to the currently active `Layer`.
      build_graph: Whether currently inside a Graph or FuncGraph.
      training: Whether currently executing in training or inference mode.
      saving: Whether currently saving to SavedModel.

    Returns:
      Context manager.
    r   )CallContextManager)r   r   rp   r   r   r   states          r   enterzCallContext.enter  s)     "E dE**r!   c                      | j                   d   S )Nr   r   r   s    r   r   zCallContext.layer  s    ;;wr!   c                      | j                   d   S )Nrp   r   r   s    r   rp   zCallContext.inputs      ;;x  r!   c                      | j                   d   S Nr   r   r   s    r   r   zCallContext.build_graph  s    ;;}%%r!   c                      | j                   d   S )Nr   r   r   s    r   r   zCallContext.training  s    ;;z""r!   c                      | j                   d   S )Nr   r   r   s    r   r   zCallContext.saving  r   r!   c                 @    | j                   d   }|sy|j                   S )Nr   F)r   r$   )r   r   s     r   frozenzCallContext.frozen  s#    KK Er!   c                     t        j                         ry| j                  xs" t        t	        j
                         dd       dk(  S )NFr   keras_graph)r   r   r   r?   r   	get_graphr   s    r   r   zCallContext.in_keras_graph  sB       "   HG%%'6-GIr!   N)__name__
__module____qualname____doc__r   r   propertyr   rp   r   r   r   r   r   r   r!   r   r   r     s    !+,     ! ! & & # # ! !   I Ir!   r   c                   "    e Zd ZdZd Zd Zd Zy)r   z"Context manager for `CallContext`.c                 4    || _         || _        |d   | _        y r   )	_call_ctxr   _build_graph)r   r   r   s      r   r   zCallContextManager.__init__	  s    DNDKm,Dr!   c                 6   | j                   }|j                  | _        |j                  | _        d|_        | j                  |_        | j
                  rG|j                  | _        |j                  xs" t        t        j                         dd       dk(  |_        y y )NTr   r   )r   r   _prev_in_callr   _prev_stater   r   _prev_in_keras_graphr?   r   r   )r   r   s     r   	__enter__zCallContextManager.__enter__  s    ~~H!))DDHkkHO "*":":d

"
" F
'##%vt
4
E  r!   c                     | j                   }| j                  |_        | j                  |_        | j
                  r| j                  |_        y y r   )r   r   r   r   r   r   r   r   )r   exc_infor   s      r   __exit__zCallContextManager.__exit__  sB    ~~H))H&&HO!%!:!:h r!   N)r   r   r   r   r   r   r   r   r!   r   r   r     s    *-
G ;r!   r   c                     t        t        | j                  dd |            }|j                  |       d|v xr |d   duS )zDReturns whether a user passed the `training` argument in `__call__`.   Nr   )r   zipr`   r~   )argspecr`   ra   	full_argss       r   training_arg_passed_to_callr   '  sH     3w||AB'./)6	y	 	FYz%:$%FFr!   c                 ~    | j                   j                  d      dk(  xr | j                   j                  d      dk(  S )zEReturns True if the object is a subclassed layer or subclassed model.zkeras.enginezkeras.layersr   findr   s    r   is_subclassedr   /  s>    





/2
5 6





/2
57r!   c                 >    | j                   j                  d      dk7  S )z6Returns whether the layer is loaded from a SavedModel.zkeras.saving.saved_modelr   r   r   s    r   from_saved_modelr   5  s    					9	:b	@@r!   c                 ,   |s6t        j                         r~t        | d      rq| j                  j                  rZ|dk(  r d}d}t        dj                  ||            |dk(  rd}d	}n|d
k(  rd}d}nd}d}t        dj                  |||            yyy)aQ  Checks that tensors passed to `add_*` method match the Keras graph.

  When one of the `add_*` method is called inside a V2 conditional branch,
  the underlying tensor gets created in a FuncGraph managed by control_flow_v2.
  We need to raise clear error messages in such cases.

  Args:
    tensor: Tensor to check, or `False` if it is known that an error
      should be raised.
    method: Caller method, one of {'add_metric', 'add_loss', 'add_update'}.
    force_raise: If an error should be raised regardless of `tensor`.

  Raises:
    RuntimeError: In case of an out-of-graph tensor.
  r   activity_regularizerag  
      class TestModel(tf.keras.Model):

        def __init__(self):
          super(TestModel, self).__init__(name='test_model')
          self.dense = tf.keras.layers.Dense(2, activity_regularizer='l2')

        def call(self, x, training=None):
          if training:
            return self.dense(x)
          else:
            return self.dense(x)
      a  
      class TestModel(tf.keras.Model):

        def __init__(self):
          super(TestModel, self).__init__(name='test_model')
          self.dense = tf.keras.layers.Dense(2, activity_regularizer='l2')

        def call(self, x, training=None):
          return self.dense(x)
      a:  You are using a layer with `activity_regularizer` in a control flow branch, e.g.:
{bad_example}
This is currently not supported. Please move your call to the layer with `activity_regularizer` out of the control flow branch, e.g.:
{correct_example}
You can also resolve this by marking your outer model/layer dynamic (eager-only) by passing `dynamic=True` to the layer constructor. Any kind of control flow is supported with dynamic layers. Note that using `dynamic=True` requires you to implement static shape inference in the `compute_output_shape(input_shape)` method.)bad_examplecorrect_example
add_metricz
      def call(self, inputs, training=None):
        if training:
          metric = compute_metric(inputs)
          self.add_metric(metric, name='my_metric', aggregation='mean')
        return inputs
      z
      def call(self, inputs, training=None):
        if training:
          metric = compute_metric(inputs)
        else:
          metric = 0.
        self.add_metric(metric, name='my_metric', aggregation='mean')
        return inputs
      add_lossz
      def call(self, inputs, training=None):
        if training:
          loss = compute_loss(inputs)
          self.add_loss(loss)
        return inputs
      z
      def call(self, inputs, training=None):
        if training:
          loss = compute_loss(inputs)
        else:
          loss = 0.
        self.add_loss(loss)
        return inputs
      z
      def call(self, inputs, training=None):
        if training:
          self.add_update(self.w.assign_add(1))
        return inputs
      z
      def call(self, inputs, training=None):
        if training:
          increment = 1
        else:
          increment = 0
        self.add_update(self.w.assign_add(increment))
        return inputs
      a  You are using the method `{method}` in a control flow branch in your layer, e.g.:
{bad_example}
This is not currently supported. Please move your call to {method} out of the control flow branch, e.g.:
{correct_example}
You can also resolve this by marking your layer as dynamic (eager-only) by passing `dynamic=True` to the layer constructor. Any kind of control flow is supported with dynamic layers. Note that using `dynamic=True` requires you to implement static shape inference in the `compute_output_shape(input_shape)` method.)methodr   r   N)r   rf   rK   r   is_control_flow_graphRuntimeErrorr   )r   r   force_raiser   r   s        r   check_graph_consistencyr   :  s      
..0vwFLL$F$F''
k	
o 	 F%  HI I 
k
o 
:	
k
o
k
o 	= >DV#+ >D >-. .q %G 1r!   c                 :    fd}t        j                  ||       S )z@Marks `outputs` as the return values for automatic control deps.c                     t        j                  |       s| S j                  |       }t        | dd      !j                  | j                        |_        nd|_        t        | dd      | j
                  |_        |S )z>Marks `tensor` as the return value for automatic control deps.r=   N_tfp_distribution)r
   
is_tf_typemark_as_returnr?   r=   r   )r   return_tensoracds     r   _mark_as_returnz'mark_as_return.<locals>._mark_as_return  s~    !!&)m &&v.Mv}d+7"%"4"4V5G5G"Hm"&m v*D1=(.(@(@m%r!   rB   )rb   r  r  s    ` r   r  r    s    ( 
		OW	55r!   c                      da y)aR  Enable the V2 dtype behavior for Keras layers.

  By default, the V2 dtype behavior is enabled in TensorFlow 2, so this function
  is only useful if `tf.compat.v1.disable_v2_behavior` has been called. Since
  mixed precision requires V2 dtype behavior to be enabled, this function allows
  you to use mixed precision in Keras layers if `disable_v2_behavior` has been
  called.

  When enabled, the dtype of Keras layers defaults to floatx (which is typically
  float32) instead of None. In addition, layers will automatically cast
  floating-point inputs to the layer's dtype.

  >>> x = tf.ones((4, 4, 4, 4), dtype='float64')
  >>> layer = tf.keras.layers.Conv2D(filters=4, kernel_size=2)
  >>> print(layer.dtype)  # float32 since V2 dtype behavior is enabled
  float32
  >>> y = layer(x)  # Layer casts inputs since V2 dtype behavior is enabled
  >>> print(y.dtype.name)
  float32

  A layer author can opt-out their layer from the automatic input casting by
  passing `autocast=False` to the base Layer's constructor. This disables the
  autocasting part of the V2 behavior for that layer, but not the defaulting to
  floatx part of the V2 behavior.

  When a global `tf.keras.mixed_precision.Policy` is set, a Keras layer's dtype
  will default to the global policy instead of floatx. Layers will automatically
  cast inputs to the policy's compute_dtype.
  TNV2_DTYPE_BEHAVIORr   r!   r   enable_v2_dtype_behaviorr    s
    > r!   c                      da y)zpDisables the V2 dtype behavior for Keras layers.

  See `tf.compat.v1.keras.layers.enable_v2_dtype_behavior`.
  FNr  r   r!   r   disable_v2_dtype_behaviorr
    s
     r!   c                  B    t         t        j                         S t         S )z1Returns True if the V2 dtype behavior is enabled.)r  r   enabledr   r!   r   v2_dtype_behavior_enabledr    s    ;;=	r!   c                   8    e Zd ZdZd Zed        Zd Zd Zd Z	y)TrackableWeightHandlera  Keras wrapper for handling tracking.Trackable object saving and restoring.

  This class handles Trackables in both V1 and V2 modes, ensuring that they can
  be saved and restored with the correct data and without adding additional ops
  on every save.

  Attributes:
    trackable: The trackable to wrap.
    num_tensors: The number of tensors that this trackable requires for saving.
  c                     t        |t        j                        st        |d      | _        t        j                          _        t        j                  |      j                         }|sd _        d  _        d  _        y t        |      dk(  rNt        |      d   }t!        j"                         rD| _        t         j%                         j&                         _         fd _         fd _        y g  _         |        _        t         j$                  j&                         _         j$                  j&                  D ]Q  }|j*                  } j(                  j-                  t/        j0                  |j2                  |j4                               S  j$                  j7                   j(                  d        _         j:                   _         fd _        y t        d	|t        |      fz        )
Nz is not a Trackable object.r   c                      y r   r   )weightss    r   <lambda>z1TrackableWeightHandler.__init__.<locals>.<lambda>  s    r!   c                      g S r   r   r   r!   r   r  z1TrackableWeightHandler.__init__.<locals>.<lambda>  s    R r!   r   c                 D    j                         j                  | d       S r   )	_saveablerestore)r  r   s    r   r  z1TrackableWeightHandler.__init__.<locals>.<lambda>+  s    t~~'7'?'?'N r!   c                  r    j                         j                  D  cg c]  } | j                   c} S c c} w r   r  specsr   specr   s    r   r  z1TrackableWeightHandler.__init__.<locals>.<lambda>,  s&    8H8N8NOO Os   4c                  j    j                   j                  D  cg c]  } | j                   c} S c c} w r   r  r  s    r   r  z1TrackableWeightHandler.__init__.<locals>.<lambda><  s#    8L8LMM Ms   0zSOnly Trackables with one Saveable are supported. The Trackable %s has %d Saveables.)ri   tracking	Trackablerg   
_trackabler   get_strategy_distribute_strategyr   saveable_objects_from_trackablevalues_num_tensors_setter_getterr   ro   r   rf   r  r  _placeholder_tensorsr   rl   r   rT   r   r,   r  
_assign_op_set_weights_v1)r   	trackable	saveablessaveabler  r   s   `     r   r   zTrackableWeightHandler.__init__  s   i!3!34)EFFDO . ; ; =D$DD68  d)dldl	Y1	i#h		0	0	2 " 0 6 67NO %'!! 4 45NN(( 	AD;;&

#
#
*
*##FLL&,,?A	A ..001J1J157++M <!3y>23 4 4r!   c                     | j                   S r   )r%  r   s    r   num_tensorsz"TrackableWeightHandler.num_tensorsB  s    r!   c           
          t        |      | j                  k7  r2t        d| j                  d| j                  dt        |      d      | j	                  |       y )NzWeight handler for trackable z0 received the wrong number of weights: expected z, got .)r   r%  rg   r   r&  )r   r  s     r   set_weightsz"TrackableWeightHandler.set_weightsF  sK    
7|t((( ??D--s7|=> > 	LLr!   c                 "    | j                         S r   )r'  r   s    r   get_tensorsz"TrackableWeightHandler.get_tensorsN  s    <<>r!   c                     i }t        |      D ]  \  }}||| j                  |   <    t        j                         j	                  | j
                  |       y r   )rq   r(  r   get_sessionrunr)  )r   r  	feed_dictidxr   s        r   r*  z&TrackableWeightHandler._set_weights_v1Q  sQ    I ) 9V28i))#./9dooy9r!   N)
r   r   r   r   r   r   r/  r2  r4  r*  r   r!   r   r  r    s0    	/4b  :r!   r  c                       e Zd ZdZd Zy)StaticTableHandlerz>Wrapper for handling weight collection for static hash tables.c                 f    d| _         || _        t        j                         | _        d }|| _        y )Nr   c                     t        d      )NzUThis layer contains a static lookup table, which cannot be changed via set_weights().)r   )rZ   s    r   raise_errorz0StaticTableHandler.__init__.<locals>.raise_error`  s     @ A Ar!   )r%  r'  r   r!  r"  r&  )r   getter_lambdar>  s      r   r   zStaticTableHandler.__init__[  s1    D DL . ; ; =DA DLr!   N)r   r   r   r   r   r   r!   r   r;  r;  X  s
    F	r!   r;  c                 v    t        j                  |       }t        d |D              rt        d|d| d      y )Nc              3   P   K   | ]  }t        |t        j                           y wr   )ri   r   RaggedTensorrL   s     r   rN   z$no_ragged_support.<locals>.<genexpr>i  s     GqA}11	2Gs   $&zLayer z; does not support RaggedTensors as input. Inputs received: z9. You can try converting your input to an uniform tensor.)r   rP   anyrg   )rp   
layer_name
input_lists      r   no_ragged_supportrF  g  s>    ||F#*GJGG
6@&J K K Hr!   c                 6    t        | d      xs t        | d      S )zGReturns True if `v` is either a PartionedVariable or a ShardedVariable._variable_list
_variablesrJ   )vs    r   is_split_variablerK  o  s    	$	%	AL)AAr!   c                 r    t        |       }t        |d      xr t        |d      xr t        | t                S )Ntrainable_weightsnon_trainable_weights)typerK   ri   )objobj_types     r   has_weightsrR  t  s=    #Y(
(/
0 $
(3
4$d#
#%r!   z8This layer's losses have been added to the parent layer.r   )F)Nr   F)Lr   r/   	threadingtensorflow.pythonr   tensorflow.python.distributer   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   r	   r
   r   r   tensorflow.python.keras.utilsr   r   r   tensorflow.python.opsr   r   r   tf_variablestensorflow.python.ops.raggedr   tensorflow.python.trackabler   r  !tensorflow.python.training.savingr   tensorflow.python.utilr   localr   r    float32VariableSynchronizationAUTOVariableAggregationNONEr:   rE   rR   rV   r\   rv   rX   ry   r   r   r   r   rr   r   r   objectr   r   r   r   r   r   r  r  r  r
  r  r  r;  rF  rK  rR  REVIVED_LOSS_PLACEHOLDERr   r!   r   <module>rg     s   F   ! 7 + . + 5 4 3 + ; 4 2 + - ; 6 8 B '	!' " !%!%!#""."F"F"K"K*>>CC"X8vC JD> #( X'v%:'
<
$*Z- VI& VIr; ;BG7A
y.x64  DP:V P:f/ KB
% @ r!   