
    BVh                     (   d 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'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 dd$l'm1Z dd%l2m3Z3 dd&l4m5Z5 dd'l4m6Z6 dd(l4m7Z7 dd)l4m8Z8 dd*l4m9Z9 dd+l4m:Z: dd,l;m<Z< dd-l=m>Z> dd.l?m@Z@ dd/lAmBZB d0ZCe5j                  e9j                  ej                  d1ZGd2 ZH G d3 d4ej                        ZJ G d5 d6eK      ZLd7 ZM eBd8d9g:      d?d;       ZN eBd<g :      d?d=       ZOd> ZPy)@z,Import a trackable object from a SavedModel.    N)logging)graph_debug_info_pb2)restore_captures)
checkpoint)checkpoint_options)
graph_view)restore)distribute_lib)distribute_utils)values_util)context)function)saved_model_utils)config)constant_op)dtypes)errors)ops)	array_ops)control_flow_assert)control_flow_ops)
lookup_ops)resource_variable_ops)	variables)fingerprinting)fingerprinting_utils)function_deserialization)load_options)load_v1_in_v2)loader_impl)path_helpers)registration)revived_types)
utils_impl)metrics)asset)autotrackable)base)data_structures)resource)trackable_utils)py_checkpoint_reader)saveable_object_util)nest)	tf_exportload_v2)r&   r*   constantc                     d} d}t        j                  t        j                  dd      | g      }t	        j
                         sAt        j                         j                  r#t        j                         j                  |       t        j                  |g      5  t        j                  t        j                        cddd       S # 1 sw Y   yxY w)zFReturns a placeholder as a handle that is not supposed to be accessed.zTrying to access a placeholder that is not supposed to be executed. This means you are executing a graph generated from the cross-replica context in an in-replica context.aY  It seems that you are trying to save a tf.types.experimental.ConcreteFunction that involves a distributed model, and the model contains parts that are loaded form a SavedModel. It's not supported to save such tf.types.experimental.ConcreteFunction. Try saving a tf.function with input_signature instead, and file a bug if there are still issues.F )shape)dtypeN)r   Assertr   placeholder_with_defaultr   executing_eagerlyr   get_default_graphbuilding_functionmark_as_unsaveablecontrol_dependenciesplaceholderr   r*   )error_messagesave_error_message	assert_ops      R/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/saved_model/load.py_unused_handlerB   Q   s    N-!  "((((b9M?L)

#
#
%  "44../AB
, 8  v78 8 8s   $C

Cc                   (     e Zd ZdZd Z fdZ xZS )_WrapperFunctiona  A class wraps a concrete function to handle different distributed contexts.

  The reason for wrapping a concrete function is because the _captured_inputs
  fields used for in-replica context and cross-replica context are different.
  When `load()` is called from within a tf.distribute.strategy scope, the
  captured inputs are distributed variables. When using these distributed
  variables during calling the function, we need different approaches when it is
  in-replica and when it is not in-replica. When it is in replica, naturally we
  should use the corresponding component of the distributed variable; when it is
  not in-replica, calling the function should mean that it is constructing a
  graph that is not actually going to be used. A typical use case is when
  constructing a functional model. In this case, return a placeholder with a
  control dependency to ensure that is never accessed.
  c                 L    | j                   j                  t        |             y N)__dict__updatevars)selfconcrete_functions     rA   __init__z_WrapperFunction.__init__x   s    MM/01    c                     d }d }t        j                         t        j                         rt	        t        ||            }nt	        t        ||            }t        |   ||      S )Nc                 H    t        j                  |       r| j                  S | S rF   )r   is_distributed_variablehandlexs    rA   
get_handlez/_WrapperFunction._call_flat.<locals>.get_handle~   s    )AA!DQXXK!KrM   c                 D    t        j                  |       r
t               S | S rF   )r   rP   rB   rR   s    rA   get_unused_handlez6_WrapperFunction._call_flat.<locals>.get_unused_handle   s"    !1!I!I!!L^ rM   )r
   get_replica_contextr   is_saving_non_distributedlistmapsuper
_call_flat)rJ   argscaptured_inputsrT   rV   	__class__s        rA   r\   z_WrapperFunction._call_flat|   sb    L 	**,8--/ S_=>oS!2ODEo7dO44rM   )__name__
__module____qualname____doc__rL   r\   __classcell__)r_   s   @rA   rD   rD   h   s    25 5rM   rD   c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Zd Zd Zd Zy)Loaderz0Helper class to load an object-based SavedModel.c                 Z   |j                   d   }|j                  | _        |j                  j                  D ci c]  }|j
                  |j                   c}| _        || _        || _	        t        j                  |j                  j                  | j                  t              | _        t               | _        || _        || _        |j&                  j(                  | _        i | _        | j$                  j,                  rt/        j0                  t2              }	| j*                  j5                         D ]M  \  }
}|
| j                  vrt7        j8                  d|
       +| j                  |
   }|	|   j;                  |       O t=        |	      | _        t?        j@                  | j                        | _!        || _"        | jG                         | _$        i | _%        tM        |t<              rg|j5                         D ]T  \  }}tM        |tN              r|| jJ                  | jH                  |   <   3|tP        f| jJ                  | jH                  |   <   V | jS                         | _*        | jW                         | _,        | j[                          |j\                  s| j_                          | j`                  D ]p  }tM        |tb        jd                        s|jg                         }ti        jj                         rCtm        jn                  tl        jp                  jr                  |       r y c c}w )Nr   )librarysaved_object_graphwrapper_functionzFConcreteFunction `%s` is listed in function alias but it is not found.):meta_graphsasset_file_def_asset_file_def	graph_defnodenameattr_operation_attributes_proto_export_dirr   load_function_def_libraryrh   rD   _concrete_functionsset_restored_concrete_functions_checkpoint_options_save_optionsmeta_info_deffunction_aliases_concrete_function_aliases"experimental_load_function_aliasescollectionsdefaultdictrY   itemsr   warnappenddictr   ObjectGraphProtoPrettyPrinter_pretty_printer_node_filters_convert_node_paths_to_ints_node_path_to_id_loaded_nodes
isinstancetuplesetattr_retrieve_all_filtered_nodes_filtered_nodes_generate_ordered_node_ids_ordered_node_ids	_load_allexperimental_skip_checkpoint_restore_checkpoint_nodesr*   CapturableResource_initializer   r8   r   add_to_collection	GraphKeysTABLE_INITIALIZERS)rJ   object_graph_protosaved_model_proto
export_dirckpt_optionssave_optionsfilters
meta_graphro   concrete_func_list_by_aliasconcrete_func_namealiasrK   	node_pathinit_ops                  rA   rL   zLoader.__init__   s   "..q1J%44D)3)=)=)B)B"D!%		499"DD$DK!D ::((00#{{-	/ 	 ),D%+D%D '1&>&>&O&OD#D<< %0$;$;D$A!'+'F'F'L'L'N E
#
eT%=%==
,,# !  445GH#E*112CDE ##>?d%CCDKKPD !D <<>DD'4 
 %]]_ Q/)TdE"AE$

T229=
>BF$

T229=
>	Q  <<>D "<<>DNN44
  K	D(55	6""$((*


 @ @'
J	KO"Ds   L(c                 `   | j                   yi }| j                   D ]  }d}t        |t              rq|j                  d      }|d   dk7  rt	        d|d    d      d}t        |dd       D ]-  \  }}| j                  ||dj                  |d|dz                }/ |||<   t        d       |S )	z?Maps all string node paths in node_filters to the int node ids.N.r   rootzWWhen passing string identifiers to node_filters, the first name must be root. Received       z)Elements in node_filters must be strings.)	r   r   strsplit
ValueError	enumerate_find_node_childjoin	TypeError)rJ   path_to_intnode_idint_node_idr   nrp   s          rA   r   z"Loader._convert_node_paths_to_ints   s    !K%% Ek	GS	!MM#&	Q<6!))21a9: :  12/ 	<GAt--4)DQqS/!:<+	<  +GCDDE rM   c                 r   | j                   yt               }t        | j                         }|r|j                  d      }| j                  |   }||v r(|j                  |       | j                  j                  |d      \  }}|9t        |t        j                        st        d| d      |j                          | j                  j                  |   j                  D ]  }| j                  j                  |j                   d      \  }}	|U|S|j#                  |j$                        }t        |t&        j(                        rd }||f| j                  |j                   <   dj+                  ||j$                        }
|j                   | j                  |
<   |j-                  |
        |rd|v ry|S )aT  Traverses through the object graph to get the IDs of all nodes to load.

    As a side-effect, if node_filters is a dictionary that contains already-
    created objects, then the children tracked by those objects will be
    added to node_filters.

    Returns:
      List of all nodes to load, or None if all nodes should be loaded.

    Nr   NNzJError when processing dictionary values passed to nodes_to_load.Object at zv is expected to be a checkpointable (i.e. 'trackable') TensorFlow object (e.g. tf.Variable, tf.Module or Keras layer).c                       y rF   r3   )r]   s    rA   <lambda>z5Loader._retrieve_all_filtered_nodes.<locals>.<lambda>,  s    rM   z{}.{})r   rw   rY   popr   addr   getr   r(   	Trackabler   _maybe_initialize_trackablers   nodeschildrenr   _lookup_dependency
local_namer)   TrackableDataStructureformatr   )rJ   all_filtered_nodesnodes_to_visitr   r   ro   setter	referencechild_object_
child_paths              rA   r   z#Loader._retrieve_all_filtered_nodes   s    !$,,-N
 $$Q'i%%i0g	&	&W%''++G\BldF		$/$+ & 
 	((*{{((1:: *),,00|-a
 D$4001E1EF,o&L&LM (F5A64JDy001^^Iy/C/CD
,5,=,=j)j)#*# H 	rM   c                     | j                   j                  |   j                  D ]  }|j                  |k(  s|j                  c S  t        d| d      )NzUnable to find node r   )rs   r   r   r   r   r   )rJ   r   
child_namepathr   s        rA   r   zLoader._find_node_child8  sW    [[&&w/88 !					+   ! +D63
44rM   c                     | j                          | j                          | j                          | j                          y)zBLoads all nodes and functions from the SavedModel and their edges.N)_load_nodes_load_edges_setup_remaining_functions+_load_checkpoint_save_and_restore_functions)rJ   s    rA   r   zLoader._load_all>  s4     	##%446rM   c                    dg}| j                         D ]J  \  }}| j                  |      }|j                  j                         t        j
                  hk(  rt        |j                        dk(  sJ t        t        |j                  j                                     }|j                  }|j                  }| j                  |      |_        | j                  |      |_        i }|j                  j                         D ]B  \  }	}|j                  }|j                  }| j                  |      | j                  |      f||	<   D t        j                   ||      |_        M y)zDRestores the checkpoint-related save/restore functions to all nodes.Nr   )_iter_all_nodesr   saveable_objectskeysr+   SERIALIZE_TO_TENSORS_NAMElennextitervaluessave_functionrestore_function_serialize_to_tensors_restore_from_tensorsr   r-   recreate_saveable_objects_self_saveable_object_factories)
rJ   temp_sessionr   protoro   saveable_object_proto
save_fn_idrestore_fn_idsaveable_fn_by_namerp   s
             rA   r   z2Loader._load_checkpoint_save_and_restore_functionsH  sU   6L..0 JXXgd				$	$	&

3
3+5 
5 5))*a/// $T%*@*@*G*G*I%J K*88
->>%)XXj%9"%)XXm%<" !+0+A+A+G+G+I 	@'D',::*/@@-'+xx
';'+xx'>'@
d
#	@ !::;N;GI 	,)JrM   c                    | j                         D ]  \  }}| j                  ||        | j                  d| j                  vr| j                  d      }| j                  D ]  }| j
                  | j                  |      }|j                  d      }|}|dd D ]8  }t        ||      st        ||| j                         d          t        ||      }: t        ||d         rt        ||d   |        yyy)z7Adds edges from objects to other objects and functions.Nr   r   r   )r   _add_object_graph_edgesr   r   r   r   r   r   hasattrr   _recreate_base_user_objectgetattr)	rJ   r   object_protor   r   loaded_noder   current_noderp   s	            rA   r   zLoader._load_edgesc  s   !%!5!5!7 :
""<9:
 'AT5I5I,IXXa[d)) 	7)kk$"7"7	"BCs#2J 	5Dt,L$(G(G(I!(LM t4,	5 |T"X.
,R+
6	7 -J'rM   c                 $   | j                   |   }| j                  |   }|j                  D ]c  } |||j                  | j                   |j                            |j                  dk(  s>t        |      rJt        t        |      dt               e y)z*Adds edges from an object to its children.__call__N)	r   _node_settersr   r   r   callabler   type_call_attribute)rJ   r   r   objr   r   s         rA   r   zLoader._add_object_graph_edgesw  sz    
++g
C(F^^ 8	S)&&I4E4E(FG 
			+HSMS	:78rM   c                     t        | j                  j                  j                               }|D ]-  }|| j                  v r| j                  || j                         / y rF   )sortedrs   concrete_functionsr   rx   _setup_function_capturesr   )rJ   concrete_function_namesrp   s      rA   r   z!Loader._setup_remaining_functions  sT    $T[[%C%C%H%H%JK' 7	22	2
##D$++67rM   c                    || j                   v ry| j                   j                  |       | j                  |   }| j                  j                  |   }|j
                  D cg c]  }||   	 }}t        j                  ||       yc c}w )z4Setup captures and variables in a restored function.N)rx   r   rv   rs   r   bound_inputsr   )rJ   concrete_function_namer   rK   r   r   inputss          rA   r   zLoader._setup_function_captures  s    !B!BB%%))*@A001GHKK**+ABE,1,>,>?eGn?F?%%&7@ @s   !Bc                 p    i }i }| j                   j                         D ]  \  }\  }}|||<   |||<    ||fS rF   )r   r   )rJ   r   node_settersr   ro   r   s         rA   _initialize_loaded_nodeszLoader._initialize_loaded_nodes  sS    EL#'#5#5#;#;#= %$eGn$l7% ,rM   c                 .   |j                   D ci c]  }|j                  |j                   }}|j                  d      }|dk(  rL|j                  j
                  }|D ]/  }| j                  j
                  |   j                  D ]  }|||<   	 1 |S |dk(  rE|j                  j                  }| j                  j
                  |   j                  D ]  }|||<   	 |S |dk(  r0|j                  D ]!  }|j                  dk(  s|j                  |d<   # |S c c}w )a  Returns a dictionary of all dependencies of an object.

    Args:
      proto: A SavedObject proto.

    Returns:
      Dict mapping string dependency name *or* int node id to the node id.
      The int node id key is used for mapping function captures.
    kindr   bare_concrete_functionr*   _create_resource)dependenciesr   r   
WhichOneofr   r   rs   r   r  r  r   )	rJ   r   refr
  r  r   fn_namebound_inputchilds	            rA   _get_node_dependencieszLoader._get_node_dependencies  s3    <A;M;MNCCNNCKK/NLNF#Dz >><<' 2';;99'BOO 	2K&1,{
#	22  
)	),,CCg77@MM 0+$/[!0  
	>> ;%11-2]],)
*; ! Os   Dc                    | j                   )t        t        | j                  j                              }nt        | j                         }t        j                  t
              }|D ]   }||   }| j                  j                  |      %| j                  j                  |   }t        | j                  |      j                               D ]S  }|j                  |       | j                   !|| j                   vs0t        d| j                  j                   |           d}|j"                  D ]T  }|j$                  }	||	   }
|
j                  |       |
j                  |j&                         ||
j                  |       |	}V # 	 t        t)        j*                  |            S # t(        j,                  $ r t        d      w xY w)z6Orders the node ids so that dependencies appear first.NzUnable to partially load SavedModel since the specified filter does not include all required objects for loading (e.g. variables used in functions or deserialization dependencies). Please include this path in the filter: zEncountered a cycle in the deserialization dependenciesin the SavedModel. This is extremely unexpected, pleasefile a bug and make sure you are not manually modifying the SavedModel.)r   ranger   rs   r   rY   r   r   r   r   rw   r  r   r   r   r   
node_namesslot_variablesslot_variable_node_idoriginal_variable_node_idr+   order_by_dependencyCyclicDependencyError)rJ   unordered_idsdependency_mapr   depsr   dep	prev_slotslot_variable_protor  	slot_depss              rA   r   z!Loader._generate_ordered_node_ids  s   #C 1 123m4//0m !,,T2N  #*G$d						(	4kk(eT007>>@A 9#C+4;O;O0O9 %%0056	89 99 i!&!5!5 *
 3 I I ##89	!,FFG  

9
%)	#*%#*H+/55nEFF00 +  * + ++s   F) )Gc              #   h   K   | j                   D ]  }|| j                  j                  |   f ! y wrF   )r   rs   r   rJ   r   s     rA   r   zLoader._iter_all_nodes  s5     )) 0T[[&&w///0s   02c                    | j                         \  }}i }| j                         D ])  \  }}|j                  D ]  }|j                  }||f||<    + | j                         D ]  \  }}|j	                  |      ||v r\||   \  }}||   }	||j
                     }
|	j                  |
|j                        }|||j                  <   t        ||j                  <   x| j                  |||      \  }}|||<   |||<    d|vr| j                         d   |d<   t        t        | j                  j                              D cg c]  }|j	                  |       c}| _        || _        yc c}w )zLoad all saved objects.N)var	slot_namer   )r  r   r  r  r   r  add_slotr$  r   	_recreater   r  r   rs   r   r   r   )rJ   r   r  slot_variable_node_idsr   r   r  r  optimizer_node_idoptimizer_objectoptimized_variableslot_variablero   r   s                 rA   r   zLoader._load_nodes  s   
 779E<
  ..0 N!&!5!5 N
 3 I I9@9L9N45NN ..0 '	7		',,1G1P.. !23"99;(11")33 2 5 <I!778BI(>>?~~eWe<fg &W#'* 	~00215eAh #(DKK,=,=(>"?A 99W% ADK%DAs    E(c                    t        j                  | j                        }t        j                  t        j                  | j                  d                  }t        j                  d      5  t        j                  |      |_        ddd       | j                  j                  r;|j                  || j                         j#                         }|j%                          n,|j                  || j                         }|j'                          |j(                  }t+        j,                         sHt/        j0                  |      }t3        |j4                        j7                         D ]  \  }}t        j8                  ||      }|j;                         }	|	rt=        d| d|	 d      |j?                  |      }
|
sVtA        jB                  |      r1tE        |
      dk(  r|
d   |_#        tI        jJ                  |
 |_#        tM        |tN        jP                        stM        |tR        jT                        r/t        jV                  t        jX                  jZ                  |
       t=        d	| d
       yy# 1 sw Y   xY w)z9Load state from checkpoint into the deserialized objects.r   CPUN)r   proto_idzmLoading a SavedModel that uses registered checkpoint saver is not supported in graph mode. The loaded object z) uses the saver registered with the name r   r   z"Unable to restore state of object z from the checkpoint.).r!   get_variables_pathrt   r   TrackableSaverr   ObjectGraphViewr   r   devicer   r1   _file_prefix_placeholderrz   allow_partial_checkpointr	   ry   expect_partialassert_nontrivial_matchassert_existing_objects_matched_checkpointr   r8   r,   NewCheckpointReaderr   object_by_proto_idr   CheckpointPositionget_registered_saver_nameNotImplementedErrorrestore_opsr   is_resource_variabler   _initializer_opr   groupr   r   LookupInterfacer*   r   r   r   r   )rJ   variables_pathsaverload_statusckptreader	object_idr   positionregistered_saverr>  s              rA   r   zLoader._restore_checkpoint$  s*   !44T5E5EFN %%j&@&@!&MNE	E	 L'2';';N'Ke$L22MM."&":":<<JN<L ))+MM.$2J2JKk113""D$$&#77Gf !!8!89??A Q.)S--7@B#==?#@@Cu E00@/ADE E
 **62"77<;1$$/Nc!$4$:$:K$Hc!3
 : :;3 ; ;< !!#--"B"BKP%4SE9NOQ Q/Q 'L Ls   ,I66J c                    t        j                         }|j                  |j                  dd |j                  D ]  }|j	                  d      \  }}d}|| j
                  v r-| j
                  |   j                  j                  j                  }|j                  |dz   |z      j                  |j                  |           |S )zFRewrite func names in the debug info by using the concrete func names.N@ )
r   GraphDebugInfofilestracesr   rv   function_def	signaturerp   CopyFrom)rJ   
debug_infooutput_debug_infokeyro   funcnew_funcs          rA   adjust_debug_info_func_namesz#Loader.adjust_debug_info_func_namesY  s    ,;;=!+!1!1A   "99S>jdDh	))	)++D1>>HHMMtczH45>>


C
 "" rM   c                 ^    t        |t              r| j                  |   }| j                  |   S rF   )r   r   r   r   r!  s     rA   r   z
Loader.getg  s+    '3%%g.g;;wrM   c                    t        j                  |j                        }|$t        j	                  |j                  d            }i }| j                  |      j                         D ]  \  }}||   ||<    |r||j                  |j                  ||| j                  | j                  | j                        }t        |t        j                        rt!        |      j"                  }	||	fS t$        }	||	fS | j'                  |||      S )ao  Creates a Python object from a SavedObject protocol buffer.

    Args:
      proto: a SavedObject proto
      node_id: int, the index of this object in the SavedObjectGraph node list.
      nodes: dict mapping int node_ids -> created objects.

    Returns:
      The recreated object, and the set-attribute function for reconnecting
      the trackable children.
    r  )r   r   r
  r   rl   operation_attributes)r"   get_registered_classregistered_name_BUILT_IN_REGISTRATIONSr   r  r  r   _deserialize_from_protoserialized_user_protort   rm   rr   r   r(   r   r   _add_trackable_childr   _recreate_default)
rJ   r   r   r   registered_classr
  rV  dep_node_idr   r   s
             rA   r&  zLoader._recreatel  s    $889N9NO044U5E5Ef5MNL 77>DDF -[,l3- 44++#%%--#99 5 ;c 
C	(c// &[ &[##E7LAArM   c           	      j     fd fdt        j                   j                  j                         fdt        j                   j                  j
                        d}j                  d      }||vr)t        d| dt        |j                                d	       ||          S )
z;Creates a Python object from a SavedObject protocol buffer.c                  <    j                  j                         S rF   )_recreate_user_objectuser_object)r   r   rJ   s   rA   r   z*Loader._recreate_default.<locals>.<lambda>  s    D..u/@/@'J rM   c                  <    j                  j                         S rF   )_recreate_functionr   )r  r   rJ   s   rA   r   z*Loader._recreate_default.<locals>.<lambda>  s    D33ENNDI rM   )r   r
  c                  :    j                   j                        S rF   )_recreate_variablevariable)r   rJ   s   rA   r   z*Loader._recreate_default.<locals>.<lambda>  s    D33ENNC rM   )ri  r   r  rn  captured_tensorr  zUnknown SavedObject type: z. Expected one of r   )
	functoolspartial _recreate_bare_concrete_functionr  _get_tensor_from_fnro  r  r   rY   r   )rJ   r   r   r  factoryr  s   ````  rA   rc  zLoader._recreate_default  s     KI"+"3"311..T#C D$,,$$e&;&;=
G F#D73D69Kw||~./q2 3 374=?rM   c                    |j                   dk(  r	 ddl}	 ddlmc m} t        j                  |      }|| j                  ||      S |S # t
        $ r+ 	 ddlmc m} n# t
        $ r}t        d      |d}~ww xY wY ^w xY w# t
        $ rE 	 ddlmc m} n7# t
        $ r+ 	 ddl	mc m} n# t
        $ r}t        d      |d}~ww xY wY nw xY wY w xY w)zInstantiates a SavedUserObject.	optimizerr   NzjError when importing Keras. Unable to load SavedModel that contains an optimizer without the Keras module.)
identifiertf_kerastf_keras.optimizers.legacy
optimizerslegacyImportError tf_keras.optimizers.optimizer_v2optimizer_v2keras.optimizers.legacykeras.optimizers.optimizer_v2r#   deserializer   )rJ   r   r   rx  r   e	looked_ups          rA   rh  zLoader._recreate_user_object  s   ;&J	J
0
0$ ))%0I,,UG<<)  	JJ88 JBCHIJJ 9	J  	J	J
-
- 	JJ55 JBCHIJJ 6	J	Js   B 	A
 
	A>	AA>	A8'A33A88A>;B =A>>B 	C	BC	C		B)(C	)	C2B>>CC	CC		CCNc                 R    ~~ G d dt         j                        } |       t        fS )Nc                       e Zd Zy)6Loader._recreate_base_user_object.<locals>._UserObjectN)r`   ra   rb   r3   rM   rA   _UserObjectr    s    
rM   r  )r'   AutoTrackabler   )rJ   r   r   r  s       rA   r   z!Loader._recreate_base_user_object  s(    w
m11  ='!!rM   c           
          t        j                  | j                        }|j                  D ]  } j	                  ||         j
                  j                  r|j                  rt         fd|j                  D              r j                  t        t        |j                                 } j                  j                  |      }t        |t              sJ t        d |D              t        d |j!                         D              k(  r| j                  |<   |t&        fS t#        j$                  d|t        d |D              t        d |j!                         D                     |t&        fS )Nc              3   :   K   | ]  }|j                   v   y wrF   )r}   ).0rp   rJ   s     rA   	<genexpr>z,Loader._recreate_function.<locals>.<genexpr>  s$      * $11
1*s   c              3   4   K   | ]  }|j                     y wrF   rp   r  fs     rA   r  z,Loader._recreate_function.<locals>.<genexpr>  s     '!qvv'   c              3   4   K   | ]  }|j                     y wrF   r  r  s     rA   r  z,Loader._recreate_function.<locals>.<genexpr>  s      /
AFF/
r  zeNot aliasing '%s' to polymorphic restored function because of mismatched concrete functions: %s vs %sc              3   4   K   | ]  }|j                     y wrF   r  r  s     rA   r  z,Loader._recreate_function.<locals>.<genexpr>  s     *Q!&&*r  c              3   4   K   | ]  }|j                     y wrF   r  r  s     rA   r  z,Loader._recreate_function.<locals>.<genexpr>  s     DQ!&&Dr  )r   recreate_functionrv   r   r   rz   r~   allr}   r   r   r|   r   r   rY   rw   _list_all_concrete_functionsr   r   r   )rJ   r   r
  fnrp   r   aliaseds   `      rA   rk  zLoader._recreate_function  s^   	!	3	3t''
)B(( 8
##D,78 <<		!	!c *..* ' //e../0
 ''++E2'4((( 'w''3 /
;;=/
 ,
 
 *,$


& w; ,,@ *'**D""A"A"CDD w;rM   c                     t        j                  || j                        }| j                  |j                  |       |t
        fS rF   )r   setup_bare_concrete_functionrv   r   r  r   )rJ   r   r
  r  s       rA   rr  z'Loader._recreate_bare_concrete_function  s=    	!	>	>t''
)B!!%">">Mw;rM   c           
         |j                   r|j                   nd }||}nd}t        j                  |j                  |j                  |j
                  |      \  }}}d }t        j                         j                  |d      5  |j                  }| j                  j                  j                         xr t        j                         xr |}	|	r]t        j                  |      5  t        j                  |j                   |j"                  ||||      t$        fcd d d        cd d d        S t        j                  |j                   |j"                  ||||      t$        fcd d d        S # 1 sw Y   nxY w	 d d d        y # 1 sw Y   y xY w)Nz"<variable loaded from saved model>r  c                 .    ~ t        j                  di |S )z8A variable creator that creates uninitialized variables.r3   )r   UninitializedVariable)next_creatorkwargss     rA   uninitialized_variable_creatorzALoader._recreate_variable.<locals>.uninitialized_variable_creator  s    
"88B6BBrM   2   )priority)r4   r5   rp   	trainablesynchronizationaggregation)rp   r   .validate_synchronization_aggregation_trainabler  r  r  r   r9   _variable_creator_scoper2  rz   experimental_variable_policy_save_variable_devicesr   get_soft_device_placementVariabler4   r5   r   )
rJ   r   rp   dbg_namer  r  r  r  saved_deviceload_with_devices
             rA   rm  zLoader._recreate_variable  s   5::Dh5h@@!!5#4#4eoo	 ,O[)
C 
			 	8	8& 
9 
 . \\l



9
9!!#(.(H(H(J
  
ZZ% 	0##KKKK!-%' )00	0 	0. .$ !!+++++#% '..%. .	0 	0 	0. . .s+   A"F'5E-	F/4F-E6	2FFc                     | j                   |j                     j                  }|j                  |j                        }|t
        fS rF   )rv   rK   graphget_tensor_by_namerp   r   )rJ   r   outer_graphro  s       rA   rs  zLoader._get_tensor_from_fn*  s=    **5+B+BCIIK!44UZZ@OG##rM   r   )r`   ra   rb   rc   rL   r   r   r   r   r   r   r   r   r   r  r  r   r   r   r   rY  r   r&  rc  rh  r   rk  rr  rm  rs  r3   rM   rA   rf   rf      s    8OKb,7r57J67(87A86+p0-&^3Qj 
#BJ&@	"&P+.Z$rM   rf   c                 &     | j                   |i |S rF   )r   )instancer]   r  s      rA   r   r   0  s    			D	+F	++rM   zsaved_model.loadzsaved_model.load_v2)v1c                     t        | t        j                        rt        j                  |       } t	        | d||      d   }|S )a  Load a SavedModel from `export_dir`.

  Signatures associated with the SavedModel are available as functions:

  ```python
  imported = tf.saved_model.load(path)
  f = imported.signatures["serving_default"]
  print(f(x=tf.constant([[1.]])))
  ```

  Objects exported with `tf.saved_model.save` additionally have trackable
  objects and functions assigned to attributes:

  ```python
  exported = tf.train.Checkpoint(v=tf.Variable(3.))
  exported.f = tf.function(
      lambda x: exported.v * x,
      input_signature=[tf.TensorSpec(shape=None, dtype=tf.float32)])
  tf.saved_model.save(exported, path)
  imported = tf.saved_model.load(path)
  assert 3. == imported.v.numpy()
  assert 6. == imported.f(x=tf.constant(2.)).numpy()
  ```

  _Loading Keras models_

  Keras models are trackable, so they can be saved to SavedModel. The object
  returned by `tf.saved_model.load` is not a Keras object (i.e. doesn't have
  `.fit`, `.predict`, etc. methods). A few attributes and functions are still
  available: `.variables`, `.trainable_variables` and `.__call__`.

  ```python
  model = tf.keras.Model(...)
  tf.saved_model.save(model, path)
  imported = tf.saved_model.load(path)
  outputs = imported(inputs)
  ```

  Use `tf.keras.models.load_model` to restore the Keras model.

  _Importing SavedModels from TensorFlow 1.x_

  1.x SavedModels APIs have a flat graph instead of `tf.function` objects.
  These SavedModels will be loaded with the following attributes:

  * `.signatures`: A dictionary mapping signature names to functions.
  * `.prune(feeds, fetches) `: A method which allows you to extract
    functions for new subgraphs. This is equivalent to importing the SavedModel
    and naming feeds and fetches in a Session from TensorFlow 1.x.

    ```python
    imported = tf.saved_model.load(path_to_v1_saved_model)
    pruned = imported.prune("x:0", "out:0")
    pruned(tf.ones([]))
    ```

    See `tf.compat.v1.wrap_function` for details.
  * `.variables`: A list of imported variables.
  * `.graph`: The whole imported graph.
  * `.restore(save_path)`: A function that restores variables from a checkpoint
    saved from `tf.compat.v1.Saver`.

  _Consuming SavedModels asynchronously_

  When consuming SavedModels asynchronously (the producer is a separate
  process), the SavedModel directory will appear before all files have been
  written, and `tf.saved_model.load` will fail if pointed at an incomplete
  SavedModel. Rather than checking for the directory, check for
  "saved_model_dir/saved_model.pb". This file is written atomically as the last
  `tf.saved_model.save` file operation.

  Args:
    export_dir: The SavedModel directory to load from.
    tags: A tag or sequence of tags identifying the MetaGraph to load. Optional
      if the SavedModel contains a single MetaGraph, as for those exported from
      `tf.saved_model.save`.
    options: `tf.saved_model.LoadOptions` object that specifies options for
      loading.

  Returns:
    A trackable object with a `signatures` attribute mapping from signature
    keys to functions. If the SavedModel was exported by `tf.saved_model.save`,
    it also points to trackable objects, functions, debug info which it has been
    saved.

  Raises:
    ValueError: If `tags` don't match a MetaGraph in the SavedModel.
  Nr   )r   osPathLikefspathload_partial)r   tagsoptionsresults       rA   loadr  4  s;    t 
BKK(:&J
D$8@&	-rM   z%__internal__.saved_model.load_partialc           	         |xs t        j                         }|%t        |t              st	        j
                  |      }t        j                  |       \  }}d}t        |j                        dk(  r|j                  d   j                  d      rt        j                  t               |j                  d   }t        j                  dk(  rt!        j"                  |dd       |Tt        |      t        |j$                  j&                        k7  r)t)        d| d|  d	|j$                  j&                   d
      |j*                  }t-        j.                  |j0                        }	t3        j4                         5  	 t7        ||| |	||      }|jA                  d      }|jC                  |      |_"        ddd       |j$                  jF                  _#        |j$                  jH                  |_$        t        jJ                  d       |jL                  r{tO        |d      rt)        d      |jP                  |_(        nR|rt)        d      t3        j4                         5  tS        jT                  | ||jV                        }||_"        ddd       t        jX                  t?        |              	 t[        j\                  |       }t        j^                  t        j`                         t        jb                  te        jf                  |      ji                                |jk                         }	 t        jx                  | |       |r"| |D ci c]  }||jA                  |       c}S diS # t8        j:                  $ r}
t=        t?        |
      dz         d}
~
ww xY w# 1 sw Y   xY w# 1 sw Y   )xY w# t<        $ r> t        j^                  t        jl                         to        jp                  d       d}Y tr        $ r? t        j^                  t        jt                         to        jv                  d       d}Y w xY w# t        jz                  $ r to        jp                  d       Y /w xY wc c}w )a  Partially load a SavedModel (saved from V2).

  Similar to `tf.saved_model.load`, but with an additional argument that
  lets you specify which nodes to load.
  `tf.saved_model.load_partial(export_dir, ["root"])` and
  `tf.saved_model.load(export_dir)` are equivalent.

  Note: This only works for SavedModels saved with TensorFlow V2 from
  `tf.saved_model.save` or Keras.

  In Tensorflow V2, SavedModel stores the **object graph** of the saved object.
  The graph contains nodes (`tf.Module`, `tf.Variable`, `tf.function`, Keras
  layers, etc.) and edges that are the name of the attributes connecting the
  objects.

  *Example 1*

  ```
  model = tf.Module()
  model.child_layer = tf.Module()
  model.child_layer.v = tf.Variable(5.)
  tf.saved_model.save(model, '/tmp/model')
  loaded = tf.__internal__.saved_model.load_partial(
  ...   '/tmp/model',
  ...   ['root.child_layer', 'root.child_layer.v'])
  loaded['root.child_layer'].v.numpy()
  5.
  loaded['root.child_layer'].v is loaded['root.child_layer.v']
  True

  *Example 2*
  model = tf.Module()
  model.child_layer = tf.Module()
  model.child_layer.v = tf.Variable(5.)
  >>>
  tf.saved_model.save(model, '/tmp/model')
  # Create a variable
  new_variable = tf.Variable(0.)
  loaded = tf.__internal__.saved_model.load_partial(
  ...   '/tmp/model',
  ...   {'root.child_layer': None, 'root.child_layer.v': new_variable})
  loaded['root.child_layer'].v.numpy()
  5.
  new_variable.numpy()
  5.
  ```

  **Loading under different distribution strategies**
  You can load different parts of the model under different distribution
  strategies. Note that this is very experimental so use with care.

  ```
  model = tf.Module()
  model.layer_1 = tf.Module()
  model.layer_1.v = tf.Variable(5.)
  model.layer_2 = tf.Module()
  model.layer_2.v = tf.Variable(7.)
  tf.saved_model.save(model, '/tmp/model')
  # Load with no strategy
  loaded = tf.__internal__.saved_model.load_partial(
  ...   '/tmp/model',
  ...   ['root.layer_1'])
  loaded['root.layer_1'].v
  <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=5.0>
  strategy = tf.distribute.MirroredStrategy()
  with strategy.scope():
  ...   loaded2 = tf.__internal__.saved_model.load_partial(
  ...     '/tmp/model',
  ...     ['root.layer_2'])
  loaded2['root.layer_2'].v
  MirroredVariable:{
      0: <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=7.0>
  }
  ```

  Args:
    export_dir: The SavedModel directory to load from.
    filters: A list or dictionary where each element or key is a string
      path to nodes that should be loaded. Node paths consist of all the child
      attribute names to reach that node in the form: `root.{attribute_name}`.
      The loader will load all of the specified nodes and their recursive
      descendants. When this option is defined, the loader will return a
      dictionary mapping the node paths to the loaded objects.
    tags: A tag or sequence of tags identifying the MetaGraph to load. Optional
      if the SavedModel contains a single MetaGraph, as for those exported from
      `tf.saved_model.save`.
    options: `tf.saved_model.LoadOptions` object that specifies options for
      loading.

  Returns:
    A dictionary mapping node paths from the filter to loaded objects.
  Nr   r   object_graph_defbiglittlez(Got an incompatible argument to `tags`: z. The SavedModel at z has one MetaGraph with tags z@. You may omit the argument, pass 'None', or pass matching tags.)experimental_io_devicez
 You may be trying to load on a different device from the computational device. Consider setting the `experimental_io_device` option in `tf.saved_model.LoadOptions` to the io_device such as '/job:localhost'.2)write_versionr|   zCould not load with experimental_load_function_aliases option because the top-level object already has an attributed with name 'function_aliases'zJSavedModels saved from Tensorflow 1.x) cannot be loaded with node filters.)saved_model_path)found_status)fingerprintz9Fingerprint not found. Saved model loading will continue.rM  zhFingerprint was found, but there was an error when reading the proto. Saved model loading will continue.)r   singleprintzSpath_and_singleprint metric could not be logged. Saved model loading will continue.r   )>r   LoadOptionsr   rw   r.   flattenr    !parse_saved_model_with_debug_infor   rk   HasFieldr%   IncrementReadApi_LOAD_V2_LABELsys	byteorderr   swap_function_tensor_contentr{   r  r   r  r   CheckpointOptionsr  r   
init_scoperf   r   NotFoundErrorFileNotFoundErrorr   r   rY  graph_debug_infotensorflow_versiontensorflow_git_versionIncrementReadr~   r   r|   r   r  r   SetReadPathr   read_fingerprintSetFoundFingerprintOnLoadkFingerprintFoundSetReadFingerprintr   to_protoSerializeToStringr  kFingerprintNotFoundr   infoRuntimeErrorkFingerprintError	exceptionSetReadPathAndSingleprintMetricException)r   r   r  r  r   rT  loadermeta_graph_defr   r   errr   r  r  r   s                  rA   r  r    s
   | 1|//1'	js3 <<D33J?  Z &	

'
'(A-##A&//0BC^,&2215N }}44^X5:<I^99>>??4TF:NL5))../ 0001 1
 (88%77&==?L		 N:*,=z$gw8 ZZ]d$AA*MdN -::MMD$$;; 	,11	)	*"
 	

 %55d 3 4 4		 )
dG@@d )d	) 
s:7, 11*=K %%73L3LM(11**,. ))+K7%%:;O
 #8?@WGVZZ((@@D>A !! :H 9 9: 	::	N N:) ) 
 %%73O3OPLLCEK	 %%73L3LM	-. K 
	 	  7LL 6 77
 Asn   :N <M.'N )N-.N: /Q
 Q9.NNNN  N*-N7:AQ AQQ
(Q65Q6c                    	 t        j                  |       }|j                  dk7  rt        j                  d|        y	 t        j                  d|        y# t
        $ r{ t        j                  d       t        j                  |       }t        |j                        dk(  r6|j                  d   j                  d      rt        j                  d|        Y yY w xY w)	a  Identifies if an exported SavedModel is a TF2 SavedModel.

  There are differences in SavedModel semantics between TF1 and TF2 that are
  documented here:
  https://www.tensorflow.org/guide/migrate/saved_model#savedmodel. This helper
  util function serves to distinguish the TF1 vs TF2 semantics used when
  exporting SavedModels.

  Args:
    export_dir: The SavedModel directory to load from.

  Returns:
    True if TF2 SavedModel semantics are used, False if TF1 SavedModel semantics
    are used.
  r   z$SavedModel at %s is a TF2 SavedModelTzAFailed to read fingerprint from SavedModel. Parsing MetaGraph ...r   r  z$SavedModel at %s is a TF1 SavedModelF)r   r  saved_object_graph_hashr   r  	Exceptionr    parse_saved_modelr   rk   r  )r   r  r   s      rA   is_tf2_saved_modelr  W  s    " 11*=K**a/ll9:F 0 
,,5zB	 
 	LLK $55jA
%%	
 ,,Q/889KLll9:F	s   :A A?CCr   )Qrc   r   rp  r  r  abslr   tensorflow.core.frameworkr    tensorflow.core.function.capturer   tensorflow.python.checkpointr   r   r   r	   tensorflow.python.distributer
   r   r   tensorflow.python.eagerr   r   ,tensorflow.python.eager.polymorphic_functionr   function_saved_model_utilstensorflow.python.frameworkr   r   r   r   r   tensorflow.python.opsr   r   r   r   r   r   tensorflow.python.saved_modelr   r   r   r   r   r    r!   r"   r#   r$   0tensorflow.python.saved_model.pywrap_saved_modelr%   tensorflow.python.trackabler&   r'   r(   r)   r*   r+   tensorflow.python.trainingr,   !tensorflow.python.training.savingr-   tensorflow.python.utilr.    tensorflow.python.util.tf_exportr/   r  AssetRestoredResourceTrackableConstantr_  rB   ConcreteFunctionrD   objectrf   r   r  r  r  r3   rM   rA   <module>r     s:   3   	 
  : = 3 ; 3 0 7 9 4 + , h . 3 . . + + 5 2 , 7 + 8 > B 6 7 5 6 6 7 I D - 5 , 7 0 7 ; B ' 6  [[))*<<> 8.*5x00 *5ZX
$V X
$v, #8"9:\ ;\~ 2r: ;D"rM   