
    2Vhn                         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)
tensorflowzkeras.layers.TFSMLayerc                   F     e Zd ZdZ	 	 	 	 	 d fd	Zd ZddZ f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              K/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/export/tfsm_layer.pyr   zTFSMLayer.__init__0   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   r/   r   replace_track_variable)r#   weightvariables      r)   r!   zTFSMLayer._add_existing_weightz   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*   )serveNTNN)F)	__name__
__module____qualname____doc__r   r!   r9   r;   __classcell__)r(   s   @r)   r	   r	      s5    $R #HT
'7) )r*   r	   N)	keras.srcr   r   keras.src.api_exportr   keras.src.export.saved_modelr   keras.src.utils.module_utilsr   r   Layerr	    r*   r)   <module>rJ      s<      - D 9 &'K) K) (K)r*   