
    BVhs                        d 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 Z(d Z)d Z*d Z+d Z,d Z-d Z. G d  d!ej^                        Z0d" Z1	 	 	 d-d#Z2d$ Z3d% Z4d& Z5d' Z6d( Z7d) Z8d* Z9d+ejt                  z  Z;d, Z<y).z$Tools for deserializing `Function`s.    N)logging)
trace_type)function_type)saved_object_graph_pb2)def_function)function)function_type_utils)
func_graph)function_def_to_graph)op_def_registry)ops)tensor)	type_spec)	array_ops)custom_gradient)default_gradient)resource_variable_ops)nested_structure_coder)compat)nest)tf_decorator)
tf_inspectc                 V    t        | t        j                  t        j                  f      S N)
isinstancer   Tensorr   BaseResourceVariable)ts    f/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/saved_model/function_deserialization.py
_is_tensorr    .   s&    	&--.CC	D
F F    c                 ,   | j                   j                  }t        j                  ||d      }t        j                  |d      }g }t        ||      D ]  \  }}t        |t        j                        r1|j                  t        j                  ||j                               Qt        |t        j                        sl|j                  |j                          | j!                  || j"                        }t        |t        j$                        ry|S )a  Calls a restored Function with structured inputs.

  This differs from `function.__call__` in that inputs and outputs are
  structured and that it casts inputs to tensors if needed.

  Note: this does not checks that non-tensor inputs match. That should be
  done before via `_concrete_function_callable_with`.

  Args:
    function: ConcreteFunction to call.
    inputs: Structured inputs compatible with
      `function.graph.structured_input_signature`.

  Returns:
    The structured function output.
  T)expand_composites
dtype_hintN)graphstructured_input_signaturer   flatten_up_toflattenzipr   r   
TensorSpecappendr   convert_to_tensordtyper   VariableSpechandle
_call_flatcaptured_inputs	Operation)	r   inputsexpected_structureflatten_inputsflatten_expectedtensor_inputsargexpectedresults	            r   _call_concrete_functionr<   5   s    "  ~~@@%%&D:.\\"4M->+;< 'mc8(F--.



?A	H3@@	A3::&' }h.F.FG&&	-r!   c                 &   	 t        j                  d      j                         5  t        j                  | |      }t        j                  |j                  |j                        cddd       S # 1 sw Y   yxY w# t        t        f$ r Y yw xY w)zDReturns None or TensorSpec obtained if `arg` is converted to tensor.guess_conversionnamer$   )shaper.   N)func_graph_lib	FuncGraph
as_defaultr   r-   r   r+   rA   r.   	TypeError
ValueError)r9   r%   r;   s      r   _try_convert_to_tensor_specrG   W   sz    		!	!'9	:	E	E	G G$$SZ@fV\\FG G G Z	  s/   $A> AA2(	A> 2A;7A> ;A> >BBc                    | j                   j                  }	 t        j                  ||      }t        |t        j                  |            D ]  \  }}t        |t        j                        r|rt        ||j                        }t        |      st        |t        j                        s y|j                  |j                  k7  r y|j                  j                  |j                        r yt        |t         j"                        r|j                  |      r yt        |      rt%        |      t%        |      k7  s y||k7  s y y# t        t
        f$ r Y yw xY w)z@Returns whether concrete `function` can be called with `inputs`.Fr$   T)r&   r'   r   r(   rE   rF   r*   r)   r   r   r+   rG   r.   r    rA   is_compatible_withr   TypeSpecid)r   r4   allow_conversionr5   r6   r9   r:   s          r    _concrete_function_callable_withrM   b   s   ~~@@''(:FCN >4<<8J+KL mc8(F--.	)#(..I_ZV5F5F%G	hnn	$^^..syy9	Hi00	1((-	C	CBxL	 	%& 
- Z	  s   E EEc           	      :   t        j                  | j                        }| j                  s|j                  r9|j                  d   dk(  r'|j                  st        d      |j                  dd }n|j                  }t        j                  ||j                  |j                  |j                  |j                  |j                  |j                        }t        j                  | j                        }t        j                   j"                  j$                  dt        j                   j"                  j&                  dt        j                   j"                  j(                  dij+                  | j,                        }t.        j                   j1                  |||	      S )
z@Deserialize a FunctionSpec object from its proto representation.r   selfzECannot deserialize a method function without a named 'self' argument.   N)argsvarargsvarkwdefaults
kwonlyargskwonlydefaultsannotationsTF)fullargspecinput_signaturejit_compile)r   decode_protorX   	is_methodrQ   NotImplementedErrorr   FullArgSpecrR   rS   rT   rU   rV   rW   rY   r   FunctionSpec
JitCompileDEFAULTONOFFgetrZ   r	   from_fullargspec_and_signature)function_spec_prototypeless_fullargspecrQ   rX   rY   rZ   s         r   '_deserialize_function_spec_as_nonmethodrh      so   /<<%%' ""$8$=$=a$@F$J$$   $$QR(D$$D&&"** &&#,,%00)88&224+ +77))+/
 ))44<<d))4477))4488% C''(	  
	)	)	H	H% 
I 
 r!   c                    |d| _         yt        j                  |j                  j                  j                         D cg c]8  }t        j                  |j                  |j                  |j                  d      : c}      }| j                  \  }}t        j                  ||j                         D ci c]  \  }}t        j                  |      | c}}|j                  i |      \  }}	t        j                   | j"                  j$                        }
t        j                  |j                  j                         |
      }|| _         yc c}w c c}}w )z@Set the FunctionType of the ConcreteFunction using FunctionSpec.N)return_annotation)_function_typefunction_type_libFunctionTyper   
parametersvalues	Parameterr@   kindoptionalr'   canonicalize_to_monomorphicitemssanitize_arg_namedefault_valuesr   
from_valuer&   structured_outputs)concrete_fnspecpunconstrained_type	arg_specskwarg_specskvinput_function_type_output_typer   s               r    set_preinitialized_function_specr      s5   	\!%K
(55 %%00779 
%
%affaffajj$
G 'AA)[,HH "'')a 
-
-a
0!
3 	q %%k&7&7&J&JK+#00$$++-#-  -+1s    =E6 Ec                     || j                      }| j                  |_        | j                  |_        | j                  d      r!t        | j                        }t        ||       |j                          |S )z1Makes a restored bare concrete function callable.function_spec)
concrete_function_nameargument_keywords_arg_keywordsallowed_positional_arguments_num_positional_argsHasFieldrh   r   r   add_to_graph)saved_bare_concrete_functionconcrete_functionsconcrete_functionr   s       r   setup_bare_concrete_functionr      s|     )"99; #44 ! #?? (!**?;;$224M$%6F  "	r!   c                   >     e Zd ZdZ fdZed        Zd Zd Z xZ	S )RestoredFunctionzhWrapper class for a function that has been restored from saved state.

  See `def_function.Function`.
  c                     t         t        |   ||d|j                         || _        |j
                  | _        |j                  | _        d| _	        y )NF)	autographrZ   T)
superr   __init__rZ   r   r   rk   rv   _default_values_omit_frequent_tracing_warning)rO   python_functionr@   r   r   	__class__s        r   r   zRestoredFunction.__init__   sZ    	
D*!--	 + /
 1D'55D(77D +/D'r!   c                      y)NF rO   s    r   _run_functions_eagerlyz'RestoredFunction._run_functions_eagerly   s     r!   c                     | j                   S r   r   r   s    r   _list_all_concrete_functionsz-RestoredFunction._list_all_concrete_functions      """r!   c                     | j                   S r   r   r   s    r   ._list_all_concrete_functions_for_serializationz?RestoredFunction._list_all_concrete_functions_for_serialization	  r   r!   )
__name__
__module____qualname____doc__r   propertyr   r   r   __classcell__)r   s   @r   r   r      s+    
/  ##r!   r   c                 $    t         j                        } fd}g } j                  D ]  }|j                  |           |D ]  }t	        ||        t        ||j                  ||      }t        j                  |||j                        S )aP  Creates a `Function` from a `SavedFunction`.

  Args:
    saved_function: `SavedFunction` proto.
    concrete_functions: map from function name to `ConcreteFunction`. As a side
      effect of this function, the `FunctionSpec` from `saved_function` is added
      to each `ConcreteFunction` in this map.

  Returns:
    A `Function`.
  c                     j                   st        d      | |f}dD ]e  }j                   D ]T  }|   }t        |j                  D cg c]  }|du  c}      rt        d      t	        |||      sFt        ||      c c S  g g }d }t        j                         D ]D  \  }	}|   }
|
j                  \  }}|j                  dj                  |	dz    ||      |             F t        d ||        d	| d
t        j                          dt        d      t        d      z   j                  |             c c}w )zECalls a restored function or raises an error if no matching function.z2Found zero restored functions for caller function.)FTNzLooks like you are trying to run a loaded non-Keras model that was trained using tf.distribute.experimental.ParameterServerStrategy with variable partitioning, which is not currently supported. Try using Keras to define your model if possible.c                 d    dj                  t        |       dj                  d | D                    S )Nz)Positional arguments ({} total):
    * {}z
    * c              3   F   K   | ]  }t        j                  |        y wr   )pprintpformat.0as     r   	<genexpr>zgrecreate_function.<locals>.restored_function_body.<locals>._pretty_format_positional.<locals>.<genexpr>G  s     @&..+@s   !)formatlenjoin)
positionals    r   _pretty_format_positionalzTrecreate_function.<locals>.restored_function_body.<locals>._pretty_format_positionalD  s0    9@@
j/
//@Z@
@B Br!   z'Option {}:
  {}
  Keyword arguments: {}rP   zUCould not find matching concrete function to call loaded from the SavedModel. Got:
  z
  Keyword arguments: z:

 Expected these arguments to match one of the following z option(s):


   )r   rF   anyr2   rM   r<   	enumerater'   r,   r   r   chrr   )rQ   kwargsr4   rL   function_namer   inpsignature_descriptionsr   indexr   r   keywordr   saved_functions                r   restored_function_bodyz1recreate_function.<locals>.restored_function_body*  s   ,,KLL F^F
 * ;)<< 
;-%m4x'?'?@t@A * + + ,Hf>NO(6:
:
;;  B
 !*.*K*K L J},];-HHj'##
5
<
<ai2:>IJJ 	8>? @X ::;<OGCGO!!"89
:		<= =/ As   	D<)decorator_argspec)
rh   r   r   r,   r   r   r   r   make_decoratorrX   )r   r   r   r   concrete_function_objectsr   cfrestored_functions   ``      r   recreate_functionr     s    4 :""$-*=X ! . A A Q$$%78N%OPQ & 8b$R78 ''='='F'F'46OQ 
	$	$%11
3 3r!   c                    t        d | j                  D              }i }i }t        j                         rt        j                         }nt        j
                         }|#dj                  t        j                               }i }i }	i }
| j                  D ]e  }|j                  st        j                         }t        j                  |j                        }|j                  ||<   ||	|<   ||
|j                  <   g i }| j                  D ]&  }t        |||      ||j                   j"                  <   ( i }t%        | |      D ]  }t'        ||||	      }d}d}|[||j(                  v rM|j(                  |   }t+        j,                  |j.                        }t+        j,                  |j0                        }|j3                         5  t5        j6                  |||      }ddd       t9        ||       ||   D ]  }||   j;                  |        d|j<                  v r|j<                  d= t?        j@                  |jB                  |jD                  |jF                  jH                        }tJ        jL                  jO                  |||j<                        }|r ||      }|j;                  |       |||<   |||j"                  <   tQ        d |jS                         D              r#|j;                  t        j
                                ||
v s|
|   }||t        j                  |      <    t        jT                  |      tW        |              |S # 1 sw Y   yxY w)a  Load a set of functions as concrete functions without captured inputs.

  Functions names are manipulated during load such that they do not overlap
  with previously created ones.

  Gradients are re-registered under new names. Ops that reference the gradients
  are updated to reflect the new registered names.

  Args:
    library: FunctionDefLibrary proto message.
    saved_object_graph: SavedObjectGraph proto message. If not passed in,
      concrete function structured signatures and outputs will not be set.
    load_shared_name_suffix: If specified, used to uniquify shared names.
      Otherwise, a unique name is generated.
    wrapper_function: An object that will be wrapped on newly created functions.

  Returns:
    Map of original function names in the library to instances of
    `ConcreteFunction` without captured inputs.

  Raises:
    ValueError: if functions dependencies have a cycle.
  c              3   H   K   | ]  }|j                   j                    y wr   )	signaturer@   )r   fdefs     r   r   z,load_function_def_library.<locals>.<genexpr>  s     Ptt~~22Ps    "Nz_load_{})r'   rx   _input_shapes)attrsc              3   :   K   | ]  }|j                   d k(    yw)TRTEngineOpN)type)r   ops     r   r   z,load_function_def_library.<locals>.<genexpr>  s     
J277m#
Js   ),setr   r   #executing_eagerly_outside_functionsGraphget_default_graphr   uidregistered_gradientsregistered_op_typer   generate_namer   as_bytesgradient_func_list_function_depsr   r@   _sort_function_defs_fix_fdef_in_placer   r   r[   canonicalized_input_signatureoutput_signaturerD   function_def_libr   _restore_gradient_functionsr   attrrl   from_structured_signaturer'   rx   function_capturescapture_typesfunction_libConcreteFunctionfrom_func_graphr   get_operationsRegisterGradient_gen_gradient_func)librarysaved_object_graphload_shared_name_suffixwrapper_functionlibrary_function_names	functionsrenamed_functionsr&   library_gradient_namesnew_gradient_op_typesgradients_to_registergdefnew_op_typeold_op_typefunction_depsr   loaded_gradients	orig_namer'   rx   protor
   depr   funcgradient_op_types                             r   load_function_def_libraryr  g  sa   6 Pw?O?OPP) 	,,.IIKE!!#E$(//	: ** >d#113kOOD$;$;<k,0,>,>[)+6K(2=D../> - >d)<$&<*>M$..%%&> !'=9 JGd"44K#8:I "&&':::
 !33I>e#9#F#F

-
-$/ 1>>

 
 " 
			 1#99
%?/1j1  
,=?OPY' .n!!*-. $))#
))O
$%??--%%$$22M
 ((88M 9 4Dd#deIi#'dii 

Jj.G.G.I
JJ --/0)).y9<@v'789,c+,-?-EFUJGX 
]1 1s   MM	c                       fd}|S )zWraps a deserialized function.c                     d }t        |j                  j                        D cg c]  \  }} |||       }}} | S c c}}w )Nc                    | | S t        j                  |      \  }}|j                         rt        j                  |      S g }|j                  !|j                         D cg c]  }|dn|
 }}t        j                  ||      S c c}w )NrP   )r   shape_and_dtypeis_fully_defined
zeros_likerankas_listr   zeros)xr   rA   r.   dimsds         r   none_to_zeroz?_gen_gradient_func.<locals>.gradient_func.<locals>.none_to_zero  s    	
%55a8leU				!**1--d		/4}}?!QYA%??__T5)) @s   "B)r*   r&   r4   )	unused_opresult_gradsr  r  r   r  s        r   r   z)_gen_gradient_func.<locals>.gradient_func  sR    *  *-\4::;L;L)M%1QL  	s   Ar   )r  r   s   ` r   r   r     s    6 
r!   c                    | j                         D ]  }|j                  dv rX|t        j                  |j                  j
                  d   j                  j                           }|j                         |_	        	 |j                  d      }||v s||   }t        |j                        |_        |j                  D cg c]  }|j                   c}|_         yc c}w # t        $ r Y w xY w)z@Populate function op's _gradient_function with default gradient.StatefulPartitionedCallPartitionedCallf_gradient_op_typeN)r   r   r   r   node_defr   r  r@   _get_gradient_function_gradient_functionget_attrr   r4   r   r   rF   )r
   r   r   r   r   r  grad_fnr   s           r   r   r     s     %%' @b 
ww@@"6??
++

3

$
$
)
)$+ ,h&==?b@%89 
-	-"#34'*299~$57YY ?c ?!@  !@  
s   ;C!>C!	C-,C-c                 |   t        j                  t              }t        j                  d       }|j                         D ]-  \  }}|D ]#  }||   j	                  |       ||xx   dz  cc<   % / | j
                  D cg c]4  }||j                  j                     dk(  r|j                  j                  6 }}g }	|rR|j                         }
|	j	                  |
       ||
   D ]&  }||xx   dz  cc<   ||   r|j	                  |       ( |rRt        |	      t        | j
                        k7  r>t        t        |j                               t        |	      z
        }t        dd| d      | j
                  D ci c]  }|j                  j                  | }}|	D cg c]  }||   	 c}S c c}w c c}w c c}w )z5Return a topologic sort of FunctionDefs in a library.c                       y)Nr   r   r   r!   r   <lambda>z%_sort_function_defs.<locals>.<lambda>2  s    r!   rP   r   z0There is a cyclic dependency between functions. zCould not resolve .)collectionsdefaultdictlistrt   r,   r   r   r@   popr   sortedr   keysrF   )r   r   edgesin_countfnamedepsr  r   readyoutputnodedestfailed_to_resolvereverser  s                  r   r   r   /  s   

!
!$
'%$$Y/("((* keT Cjuoo ""
	$..%%	&!	+ nn% 
 &99;D
MM$d tnnd^T 	 	[C(())s8==?3c&kAB
G)*;)<A>@ @ 4;3C3CD4T^^  $&D'D$	%'!*	%%)& E	%s   >9F/<F4 F9c                 n    d| j                   v r'| j                  dvr| j                   d   j                  S y)z$Returns the custom gradient op type.r  r  N)r   r   s)r  s    r   _get_gradient_op_typer7  O  s4    X]]*kkGG==,-///	r!   c                    | j                   |v r|| j                      j                  | _         | j                  j                         D ]  \  }}|j	                  d      dk(  r3||j
                  j                     j                  |j
                  _        M|j	                  d      dk(  sb|j                  j
                  D ]   }||j                     j                  |_        "  | j                   dk(  rad| j                  vs| j                  d   j                  s:d| j                  d   _        |dj                  t        j                               z  }t        j                  | j                         }|rt        d |j                  D        d	      }|rd	}d
| j                  v r3| j                  d
   j                  r| j                  d
   j                  }n?|j                  j                  r)t!        j"                  |j                  j                        }|st!        j"                  | j                        }|t!        j"                  |      z   | j                  d
   _        y	y	y	)z7Replace functions calls and shared names in `node_def`.valuer  r'  HashTableV2use_node_name_sharingTz_{}c              3   @   K   | ]  }|j                   d k(  s|  yw)shared_nameNr?   r   s     r   r   zfix_node_def.<locals>.<genexpr>q  s     Cq166]+BCs   Nr=  )r   r@   r   rt   
WhichOneofr  r'  br   r   r   r   rd   nextr6  default_valuer   r   )	r  r   shared_name_suffixr   
attr_valuefnop_defr   r=  s	            r   fix_node_defrF  W  s   [[IHKK(--HK}}**, *maW%/&z';';<AAjoo			w	'6	1$$ *"BGG$))*	* [[M!x}}4MM124415hmm+,. ELL33 x{{+&CFKKCTJDk	(--	'HMM-,H,J,JmmM244ood&8&8&:&:;oohmm4 (:;
; mmM"$  r!   c                    | j                   j                  }d}| j                  D ]N  }t        |||       t	        |      }|||v r+t        j                  ||         |j                  d   _        Md}P |r*t        j                  d| j                   j                         t        | j                   j                        | j                   _        |S )a  Fixes a FunctionDef proto to be loaded in current context.

  In particular, when loading a function library into an eager context, one
  must rename the functions to avoid conflicts with existent functions.

  Args:
    fdef: FunctionDef proto to fix. It is mutated in-place.
    functions: map from function name to a ConcreteFunction instance.
    shared_name_suffix: A unique string for this load which helps to avoid
      `shared_name` collisions across loads. Two functions from the same load
      using the same `shared_name` still need to share, but functions from
      different loads with the same `shared_name` should not.
    new_gradient_op_types: map from old gradient op type to newly generated op
      type.

  Returns:
    orig_name: original value of fdef.signature.name
  Fr  TznImporting a function (%s) with ops with unsaved custom gradients. Will likely fail if a gradient is requested.)r   r@   r  rF  r7  r   r   r   r6  r   warning_clean_function_name)r   r   rB  r   r  !contains_unsaved_custom_gradientsr  op_types           r   r   r     s    ( nn!!)&+#-- 1h9&89#H-G	)	)/5!'*0,)*, -1)1 'OO	348NN4G4GI -T^^-@-@A$..	r!   c                 *   t               }| j                  D ]  }t        |      }|j                  |v r|j	                  |j                         8|r||v r|j	                  ||          S|j
                  j                         D ]  \  }}|j                  d      dk(  r&|j	                  |j                  j                         @|j                  d      dk(  sU|j                  j                  D ]  }|j	                  |j                            |S )z$Find functions referenced in `fdef`.r9  r  r'  )r   r  r7  r   addr   rt   r>  r  r@   r'  )	r   r   r   r.  r  grad_op_typer   rC  rD  s	            r   r   r     s    
 
$-- h(2L{{,,
hhx{{	,*@@
hh%l34#==..0 -!Z  )V3
((:??''
(""7+v5OO(( bHHRWW	 
+r!   z^%s(.*)_\d+$c                 `    t        j                  t        |       }|r|j                  d      S | S )z:Vanity function to keep the function names comprehensible.rP   )research_FUNCTION_WRAPPER_NAME_REGEXgroup)r@   matchs     r   rI  rI    s+     ))0$
7%
;;q>Kr!   )NNN)=r   r%  r   rP  abslr   tensorflow.core.functionr   %tensorflow.core.function.polymorphismr   rl   tensorflow.core.protobufr   tensorflow.python.eagerr   r   r   ,tensorflow.python.eager.polymorphic_functionr	   tensorflow.python.frameworkr
   rB   r   r   r   r   r   r   tensorflow.python.opsr   r   r   r   tensorflow.python.saved_modelr   tensorflow.python.utilr   r   r   r   r    r<   rG   rM   rh   r   r   Functionr   r   r  r   r   r   r7  rF  r   r   _INFERENCE_PREFIXrR  rI  r   r!   r   <module>ra     s    +   	  / T ; 0 < L D Q 7 + . 1 + 1 2 7 @ ) ' / -FD<&T-L&'#|,, '#TW3v 266:/3NbB@,&@%=P&R.  /,2P2P  3 r!   