
    ijy                         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d       G d d	ej                               Zy
)    )backend)layers)keras_export)_list_variables_used_by_fns)serialization_lib)
tensorflowzkeras.layers.TFSMLayerc                   X     e Zd ZdZ	 	 	 	 	 d fd	Zd ZddZ fdZed	d       Z	 xZ
S )
	TFSMLayerag  Reload a Keras model/layer that was saved via SavedModel / ExportArchive.

    Arguments:
        filepath: `str` or `pathlib.Path` object. The path to the SavedModel.
        call_endpoint: Name of the endpoint to use as the `call()` method
            of the reloaded layer. If the SavedModel was created
            via `model.export()`,
            then the default endpoint name is `'serve'`. In other cases
            it may be named `'serving_default'`.

    Example:

    ```python
    model.export("path/to/artifact")
    reloaded_layer = TFSMLayer("path/to/artifact")
    outputs = reloaded_layer(inputs)
    ```

    The reloaded object can be used like a regular Keras layer, and supports
    training/fine-tuning of its trainable weights. Note that the reloaded
    object retains none of the internal structure or custom methods of the
    original object -- it's a brand new layer created around the saved
    function.

    **Limitations:**

    * Only call endpoints with a single `inputs` tensor argument
    (which may optionally be a dict/tuple/list of tensors) are supported.
    For endpoints with multiple separate input tensor arguments, consider
    subclassing `TFSMLayer` and implementing a `call()` method with a
    custom signature.
    * If you need training-time behavior to differ from inference-time behavior
    (i.e. if you need the reloaded object to support a `training=True` argument
    in `__call__()`), make sure that the training-time call function is
    saved as a standalone endpoint in the artifact, and provide its name
    to the `TFSMLayer` via the `call_training_endpoint` argument.
    c           	      h   t        j                          dk7  rt        d      t        |   |||       t        j
                  j                  |      | _        || _        || _	        || _
        t        | j                  |      rt        | j                  |      | _        ns|| j                  j                  v r| j                  j                  |   | _        n<t        d| dt!        | j                  j                  j#                                      |rt        | j                  |      rt        | j                  |      | _        ns|| j                  j                  v r| j                  j                  |   | _        n<t        d| dt!        | j                  j                  j#                                      | j                  g}|r|j'                  | j$                         t)        |      \  }}	|D ]  }
| j+                  |
        |	D ]  }
| j+                  |
        | j-                          y )Nr   zFThe TFSMLayer is only currently supported with the TensorFlow backend.)	trainablenamedtypezThe endpoint 'z' is neither an attribute of the reloaded SavedModel, nor an entry in the `signatures` field of the reloaded SavedModel. Select another endpoint via the `call_endpoint` argument. Available endpoints for this SavedModel: z' is neither an attribute of the reloaded SavedModel, nor an entry in the `signatures` field of the reloaded SavedModel. Available endpoints for this SavedModel: )r   NotImplementedErrorsuper__init__tfsaved_modelload_reloaded_objfilepathcall_endpointcall_training_endpointhasattrgetattrcall_endpoint_fn
signatures
ValueErrorlistkeyscall_training_endpoint_fnappendr   _add_existing_weight_build_at_init)selfr   r   r   r   r   r   all_fnstvsntvsv	__class__s              p/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/export/tfsm_layer.pyr   zTFSMLayer.__init__1   s     ??,%&  	94uE^^00: *&<# 4%%}5$+D,>,>$ND!d00;;;$($6$6$A$A-$PD!  0$ **55::<=>@  "t))+AB18&&(>2. (4+=+=+H+HH151C1C1N1N*2. !$%;$< =(
 D..99>>@ABD  (()!NN499:/8	T 	)A%%a(	) 	)A%%a(	) 	    c           
          t        j                  ||j                  |j                  |j                  |j
                  j                  dd            }| j                  |       y)zTracks an existing weight./_)initializerr   r   shaper   N)r   Variabler   r   r0   r   replace_track_variable)r$   weightvariables      r*   r"   zTFSMLayer._add_existing_weight{   sP    ##&&,,,,$$S#.
 	X&r+   c                 j    |r| j                   r | j                  |fi |S  | j                  |fi |S )N)r   r    r   )r$   inputstrainingkwargss       r*   callzTFSMLayer.call   s>    **5t55fGGG$t$$V6v66r+   c                 t    t         |          }| j                  | j                  | j                  d}i ||S )N)r   r   r   )r   
get_configr   r   r   )r$   base_configconfigr)   s      r*   r<   zTFSMLayer.get_config   sD    g(* !//&*&A&A	
 )+(((r+   c                 `    ||nt        j                         }|durt        d       | di |S )a/  Creates a TFSMLayer from its config.
        Args:
            config: A Python dictionary, typically the output of `get_config`.
            custom_objects: Optional dictionary mapping names to custom objects.
            safe_mode: Boolean, whether to disallow loading TFSMLayer.
                When `safe_mode=True`, loading is disallowed because TFSMLayer
                loads external SavedModels that may contain attacker-controlled
                executable graph code. Defaults to `True`.
        Returns:
            A TFSMLayer instance.
        Fah  Requested the deserialization of a `TFSMLayer`, which loads an external SavedModel. This carries a potential risk of arbitrary code execution and thus it is disallowed by default. If you trust the source of the artifact, you can override this error by passing `safe_mode=False` to the loading function, or calling `keras.config.enable_unsafe_deserialization(). )r   in_safe_moder   )clsr>   custom_objects	safe_modeeffective_safe_modes        r*   from_configzTFSMLayer.from_config   sK      $ "//1 	 e+A  }V}r+   )serveNTNN)F)NN)__name__
__module____qualname____doc__r   r"   r:   r<   classmethodrF   __classcell__)r)   s   @r*   r
   r
   	   sD    $R #HT
'7)  r+   r
   N)	keras.srcr   r   keras.src.api_exportr   keras.src.export.saved_modelr   keras.src.savingr   keras.src.utils.module_utilsr   r   Layerr
   r@   r+   r*   <module>rT      s?      - D . 9 &'l l (lr+   