
    ~WhmN                        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Zddlm	c 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eeefZ ej                    Z ej                    Z  G d	 d
          Z! G d d          Z" ed          d             Z#d Z$ G d d          Z%d Z&d Z'd Z(d Z)d Z*d Z+	 ddZ,	 ddZ-dS )z6Object config serialization and deserialization logic.    N)object_registration)serialization)in_tf_saved_model_scope)generic_utils)	tf_export)keras_exportc                       e Zd Zd Zd ZdS )Configc                     || _         d S N)config)selfr   s     d/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/keras/saving/serialization_lib.py__init__zConfig.__init__)   s        c                 *    t          | j                  S r   )serialize_keras_objectr   r   s    r   	serializezConfig.serialize,   s    %dk222r   N)__name__
__module____qualname__r   r    r   r   r
   r
   (   s2          3 3 3 3 3r   r
   c                   &    e Zd ZdZddZd Zd ZdS )SafeModeScopezBScope to propagate safe mode flag to nested deserialization calls.Tc                     || _         d S r   )	safe_mode)r   r   s     r   r   zSafeModeScope.__init__3   s    "r   c                 N    t                      | _        | j        t          _        d S r   )in_safe_modeoriginal_valuer   	SAFE_MODEr   s    r   	__enter__zSafeModeScope.__enter__6   s    *nn"n	r   c                 (    | j         t          _        d S r   )r    r!   r   r   argskwargss      r   __exit__zSafeModeScope.__exit__:   s    "1	r   N)T)r   r   r   __doc__r   r"   r'   r   r   r   r   r   0   sL        LL# # # #- - -2 2 2 2 2r   r   z0keras.__internal__.enable_unsafe_deserializationc                      dt           _        dS )zADisables safe mode globally, allowing deserialization of lambdas.FN)r!   r   r   r   r   enable_unsafe_deserializationr*   >   s      Ir   c                  .    t          t          dd           S )Nr   )getattrr!   r   r   r   r   r   D   s    9k4000r   c                       e Zd ZdZd Zd ZdS )ObjectSharingScopez?Scope to enable detection and reuse of previously seen objects.c                 N    dt           _        i t           _        i t           _        d S NTSHARED_OBJECTSenabledid_to_obj_mapid_to_config_mapr   s    r   r"   zObjectSharingScope.__enter__K   s     !%')$*,'''r   c                 N    dt           _        i t           _        i t           _        d S )NFr1   r$   s      r   r'   zObjectSharingScope.__exit__P   s     !&')$*,'''r   N)r   r   r   r(   r"   r'   r   r   r   r.   r.   H   s8        II- - -
- - - - -r   r.   c                 r    t          t          dd          r t          j                            | d          S dS )z:Retrieve an object previously seen during deserialization.r3   FN)r,   r2   r4   get)obj_ids    r   get_shared_objectr:   V   s9    ~y%00 >+//===> >r   c                     t          t          dd          sdS t          t          |                     }|t          j        vr|t          j        |<   dS ||d<   t          j        |         }||d<   dS )z>Call after serializing an object, to keep track of its config.r3   FNshared_object_id)r,   r2   intidr5   )objr   r9   prev_configs       r   !record_object_after_serializationrA   \   sv    >9e44 C\\F^44428'///%+!"$5f=*0&'''r   c                 T    t          t          dd          sdS | t          j        |<   dS )zFCall after deserializing an object, to keep track of it in the future.r3   FN)r,   r2   r4   )r?   r9   s     r   #record_object_after_deserializationrC   i   s.    >9e44 +.N (((r   c                 v   t           j        j                                        rt	                      rt          j        |           S | | S t          | t                    r| S t          | t          t          f          rd | D             S t          | t                    rt          |           S t          | t                    rdd|                     d          idS t          | t           j                  r|                                 S t          | t           j                  r6d|                                                                 | j        j        ddS t-          |           j        t0          j        k    rRt          | t0          j                  r$d	|                                 | j        j        ddS |                                 S t          | t           j                  r| j        S t          | t:          j                  rO| j        d
k    rDt?          j         dtC          j"        |            d           ddtG          j$        |           idS t          | t           j%                  rN| &                                }t          tO          d |                    }d| j(        j        | j(        j        |ddS tS          j*        | j(        d          }|t          | t:          j                  r| j        }n| j(        j        }| j(        j        }|dk    rd}nt          | t:          j                  rtW          j,        |           }nVtW          j,        | j(                  }n<|-                    d          }d.                    |dd                   }|d         }d}||t_          |           |d}ta          | d          r(| 1                                }|t          |          |d<   ta          | d          r(| 2                                }	|	t          |	          |d<   tg          | |           |S )a  Retrieve the config dict by serializing the Keras object.

    `serialize_keras_object()` serializes a Keras object to a python dictionary
    that represents the object, and is a reciprocal function of
    `deserialize_keras_object()`. See `deserialize_keras_object()` for more
    information about the config format.

    Args:
      obj: the Keras object to serialize.

    Returns:
      A python dict that represents the object. The python dict can be
      deserialized via `deserialize_keras_object()`.
    Nc                 ,    g | ]}t          |          S r   r   ).0xs     r   
<listcomp>z*serialize_keras_object.<locals>.<listcomp>   s!    777a&q))777r   	__bytes__valueutf-8)
class_namer   
__tensor__)rK   dtype	__numpy__z<lambda>a  The object being serialized includes a `lambda`. This is unsafe. In order to reload the object, you will have to pass `safe_mode=False` to the loading function. Please avoid using `lambda` in the future, and use named Python functions instead. This is the `lambda` being serialized:    )
stacklevel
__lambda__c                     t          | t          j                  r|                                 n"t          | t          j                  r| j        n| S r   )
isinstancetfTensorShapeas_listDTypenamerH   s    r   <lambda>z(serialize_keras_object.<locals>.<lambda>   sB    a00@!))+++ *1bh 7 7>affQ r   __typespec__)rM   	spec_namemoduler   registered_namekeras)api_namebuiltins.)r_   rM   r   r`   get_build_configbuild_configget_compile_configcompile_config)4rV   __internal__tf2r3   r   legacy_serializationr   rU   PLAIN_TYPESlisttupledictserialize_dictbytesdecoderW   rX   TensornumpytolistrO   rZ   typer   npr   ndarrayitemrY   typesFunctionTypewarningswarninspect	getsourcer   	func_dumpTypeSpec
_serializemap	__class__r   get_canonical_name_for_symbolr   get_registered_namesplitjoin_get_class_or_fn_confighasattrrf   rh   rA   )
r?   	ts_configkeras_api_namer_   rM   r`   partsr   rg   ri   s
             r   r   r   p   sz   $ ?&&(( @,C,E,E @#:3???
{
#{## 
#e}%% 87737777#t #c""" #u 
%

7 3 34
 
 	
 #r~&& {{}}#ry!! 
&++-- 
 
 	
 Cyyr{**c2:&& 
	) ZZ\\ Y^    88::#rx   x#u)** 
s|z/I/IO
 7>6G6L6LO O 	
 	
 	
 	
 '055
 
 	
 #r{## 
NN$$	@ @ 	 
 
	 )/m.#
 
 	
 <  N c5-.. 	.^FF]-F]+
Z"OO#u122 "5"I#"N"N"5"IM# #
 $$S))%*%%2Y
 )#..*	 F s&'' B++--#%3L%A%AF>"s()) F//11%'5n'E'EF#$%c6222Mr   c                 D   t          | t          j                  r| j        S t	          | d          rM|                                 }t          |t                    st          d|  d|           t          |          S t          d|  dt          |            d          )z1Return the object's config depending on its type.
get_configzThe `get_config()` method of z$ should return a dict. It returned: zCannot serialize object z	 of type zG. To be serializable, a class must implement the `get_config()` method.)
rU   r{   r|   r   r   r   rp   	TypeErrorrq   rw   )r?   r   s     r   r   r      s     #u)** |sL!! 
!!&$'' 	1 1 1(.1 1   f%%%@s @ @T#YY @ @ @
 
 	
r   c                 >    d |                                  D             S )Nc                 4    i | ]\  }}|t          |          S r   rF   )rG   keyrK   s      r   
<dictcomp>z"serialize_dict.<locals>.<dictcomp>  s'    MMM:3C'..MMMr   )items)r?   s    r   rq   rq     s    MMMMMMr   Tc                    t                      }||n|                    dd          }pi t          j        j                                        rt                      rt          j        | |          S | dS t          | t                    r4t          | t                    r                    |           |          S | S t          | t          t          f          rfd| D             S t          | t                    st!          d|            d| vsd| vr!fd|                                 D             S | d         }| d         }|dk    r"t          j        |d	         |d
                   S |dk    r"t'          j        |d	         |d
                   S | d         dk    r|d	                             d          S | d         dk    r+rt-          d          t/          j        |d	                   S | d         dk    rZt3          | d         | d         | d         d|           }t5          d |          }|                    t          |                    S |                     dd          }	|                     d|          }
|dk    r|}t3          ||
|	d|           S d| v rt9          | d                   }||S t3          ||
|	d|           }t;          |d          st!          d| d|            t=          j                  }tA                    }|5  |5  |!                    |          }|                     dd          }|r|"                    |           |                     dd          }|r|#                    |           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   d| v rtI          || d                    |S )a  Retrieve the object by deserializing the config dict.

    The config dict is a Python dictionary that consists of a set of key-value
    pairs, and represents a Keras object, such as an `Optimizer`, `Layer`,
    `Metrics`, etc. The saving and loading library uses the following keys to
    record information of a Keras object:

    - `class_name`: String. This is the name of the class,
      as exactly defined in the source
      code, such as "LossesContainer".
    - `config`: Dict. Library-defined or user-defined key-value pairs that store
      the configuration of the object, as obtained by `object.get_config()`.
    - `module`: String. The path of the python module, such as
      "keras.engine.compile_utils". Built-in Keras classes
      expect to have prefix `keras`.
    - `registered_name`: String. The key the class is registered under via
      `keras.utils.register_keras_serializable(package, name)` API. The key has
      the format of '{package}>{name}', where `package` and `name` are the
      arguments passed to `register_keras_serializable()`. If `name` is not
      provided, it defaults to the class name. If `registered_name` successfully
      resolves to a class (that was registered), the `class_name` and `config`
      values in the dict will not be used. `registered_name` is only used for
      non-built-in classes.

    For example, the following dictionary represents the built-in Adam optimizer
    with the relevant config:

    ```python
    dict_structure = {
        "class_name": "Adam",
        "config": {
            "amsgrad": false,
            "beta_1": 0.8999999761581421,
            "beta_2": 0.9990000128746033,
            "decay": 0.0,
            "epsilon": 1e-07,
            "learning_rate": 0.0010000000474974513,
            "name": "Adam"
        },
        "module": "keras.optimizers",
        "registered_name": None
    }
    # Returns an `Adam` instance identical to the original one.
    deserialize_keras_object(dict_structure)
    ```

    If the class does not have an exported Keras namespace, the library tracks
    it by its `module` and `class_name`. For example:

    ```python
    dict_structure = {
      "class_name": "LossesContainer",
      "config": {
          "losses": [...],
          "total_loss_mean": {...},
      },
      "module": "keras.engine.compile_utils",
      "registered_name": "LossesContainer"
    }

    # Returns a `LossesContainer` instance identical to the original one.
    deserialize_keras_object(dict_structure)
    ```

    And the following dictionary represents a user-customized `MeanSquaredError`
    loss:

    ```python
    @keras.utils.register_keras_serializable(package='my_package')
    class ModifiedMeanSquaredError(keras.losses.MeanSquaredError):
      ...

    dict_structure = {
        "class_name": "ModifiedMeanSquaredError",
        "config": {
            "fn": "mean_squared_error",
            "name": "mean_squared_error",
            "reduction": "auto"
        },
        "registered_name": "my_package>ModifiedMeanSquaredError"
    }
    # Returns the `ModifiedMeanSquaredError` object
    deserialize_keras_object(dict_structure)
    ```

    Args:
        config: Python dict describing the object.
        custom_objects: Python dict containing a mapping between custom
            object names the corresponding classes or functions.
        safe_mode: Boolean, whether to disallow unsafe `lambda` deserialization.
            When `safe_mode=False`, loading an object has the potential to
            trigger arbitrary code execution. This argument is only
            applicable to the Keras v3 model format. Defaults to True.

    Returns:
      The object described by the `config` dictionary.

    Nmodule_objectsc                 4    g | ]}t          |           S )custom_objectsr   deserialize_keras_object)rG   rH   r   r   s     r   rI   z,deserialize_keras_object.<locals>.<listcomp>  sA     
 
 
  %.I  
 
 
r   zCould not parse config: rM   r   c                 <    i | ]\  }}|t          |           S r   r   )rG   r   rK   r   r   s      r   r   z,deserialize_keras_object.<locals>.<dictcomp>  sH     
 
 
 U )n	  
 
 
r   rN   rK   rO   )rO   rP   rJ   rL   rS   a  Requested the deserialization of a `lambda` object. This carries a potential risk of arbitrary code execution and thus it is disallowed by default. If you trust the source of the saved model, you can pass `safe_mode=False` to the loading function in order to allow `lambda` loading.r]   r^   r`   r_   class)obj_typefull_configr   c                     t          | t                    rt          j        |           n=t	          t          j        t          |                     rt          t          |           n| S r   )rU   rn   rV   rW   r   dtypesstrr,   r[   s    r   r\   z*deserialize_keras_object.<locals>.<lambda>  sO    !T""GbnQ'''$+BIs1vv$>$>E'"a...A r   functionr<   from_configz&Unable to reconstruct an instance of 'zM' because the class is missing a `from_config()` method. Full object config: rg   ri   )%r   poprV   rj   rk   r3   r   rl   r   rU   rm   r   r8   rn   ro   rp   r   r   constantrx   arrayencode
ValueErrorr   	func_load_retrieve_class_or_fnr   _deserializer:   r   r   custom_object_scoper   r   build_from_configcompile_from_configrC   )r   r   r   r&   safe_scope_argr   rM   inner_configr?   r_   r`   fn_nameclscustom_obj_scopesafe_mode_scopeinstancerg   ri   s    ``               r   r   r     sg   J "^^N"0"<)IZZ 0$77N#)rN
 ?&&(( 
,C,E,E 
#<NN
 
 	
 ~t&+&& fc"" 	*~'9'9&'A'A'M "&))&4-(( 

 
 
 
 
 	
 
 
 	
 fd## =;6;;<<<6!!XV%;%;
 
 
 
 
 %llnn	
 
 
 	
 %J(#L \!!{<0W8MNNNN[  xW-\'5JKKKKl{**G$++G444l|++ 	K   &|G'<===l~--#;$%8)
 
 
 G G 	
 
 l 3 3444 ZZ$''Fjj!2J??OZ$)
 
 
 	
 V##'9 :;;?J
%  C 3&& 
,Z , ,#), ,
 
 	
 +>~NN#I..O	 9 9? 9 9??<00zz.$77 	5&&|444$4d;; 	9((8889 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 V##+f/0	
 	
 	
 Os7   )N?,A0N(N?(N,	,N?/N,	0N??OOc                    t          j        ||          }||S |r|dk    s|                    d          rt          j        |dz   | z             }||S 	 t          j        |          }n)# t          $ r t          d| d|  d| d|           w xY wt          |          
                    | d           }||S t          d	| d|  d
|           )N)r   ra   zkeras.rd   zCould not deserialize z 'z' because its parent module z) cannot be imported. Full object config: zCould not locate zo'. Make sure custom classes are decorated with `@keras.utils.register_keras_serializable`. Full object config: )r   get_registered_object
startswithr   get_symbol_from_name	importlibimport_moduleModuleNotFoundErrorr   varsr8   )	rZ   r`   r_   r   r   r   
custom_objr?   mods	            r   r   r     sW   
 %:  J  
 W 1 1( ; ;0#1DEEC
	)&11CC" 	 	 	5 5 5T 5 5%+5 5'25 5  	 3iimmD$''?J
	-H 	- 	- 	- 	-  +	- 	-  s   A, ,&Br0   r   ).r(   r   r   	threadingr{   r}   ru   rx   tensorflow.compat.v2compatv2rV   keras.savingr   keras.saving.legacyr   rl   %keras.saving.legacy.saved_model.utilsr   keras.utilsr   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   r   r=   floatboolrm   localr2   r!   r
   r   r*   r   r.   r:   rA   rC   r   r   rq   r   r   r   r   r   <module>r      sT   = <                ! ! ! ! ! ! ! ! ! , , , , , , E E E E E E I I I I I I % % % % % % - , , , , , 9 9 9 9 9 9C% ""IO	3 3 3 3 3 3 3 32 2 2 2 2 2 2 2 @AA    BA 
1 1 1- - - - - - - -> > >
1 
1 
1/ / /I I IX
 
 
,N N N
 ,0l l l l` JN( ( ( ( ( (r   