
    BVh;"                         d 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y)z"Tools for serializing `Function`s.    )function_type)saved_object_graph_pb2)function)wrap_function)function_type_utils)
func_graph)nested_structure_coder)nestc                    | j                   j                  r'| j                   j                  d   dk(  rt        d      t        j                         }|j                   j                  t        j                  | j                   j                  i                    d|_	        |j                  j                  t        j                  | j                               t        j                  j                  j                  t        j                  j                  j                  t        j                  j                  j                  dj                  | j                         |_        |S )z>Serialize a FunctionSpec object into its proto representation.r   selfz<Can not serialize tf.function with unbound 'self' parameter.)annotationsF)NTF)fullargspecargs	TypeErrorr   FunctionSpecCopyFromr	   encode_structure_replace	is_methodinput_signature
JitCompileDEFAULTONOFFgetjit_compile)function_specprotos     d/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/saved_model/function_serialization.py_serialize_function_specr       s     $$

#
#
(
(
+v
5
F  !
-
-
/% --

#
#
,
,
,
<>? %/  --m.K.KLN
 #//::BB"//::==#00;;?? C!!"	  
,    c                    g }	 | j                   D ]  }|j                  ||           	 t	        j
                         }t        j                  | j                        }|j                  j                  t        j                  | j                               |j                  j                  t        j                  |             |j                  j!                  |       |S # t        $ r t        d| j                   dd      w xY w)zBuild a SavedConcreteFunction.z!Failed to add concrete function 'z3' to object-based SavedModel as it captures tensor z which is unsupported or not reachable from root. One reason could be that a stateful object or a variable that the function depends on is not assigned to an attribute of the serialized trackable object (see SaveTest.test_captures_unreachable_variable).)captured_inputsappendKeyErrornamer   SavedConcreteFunctionfunc_graph_moduleconvert_structure_to_signaturestructured_outputscanonicalized_input_signaturer   r	   r   structured_input_signatureoutput_signaturebound_inputsextend)concrete_functionnode_idsr.   captureconcrete_function_protor*   s         r   serialize_concrete_functionr4   >   s   ,
O$44 -(7+,- 3HHJ(GG**,77@@--

6
689 **33--.@AC&&--l;	  # 
 O

+,=,B,B+C D229 =N	NO OOs   %C   &Dc                 "   | j                   t        | t        j                        ry| j                  }|yt        j                  |j                  j                         D cg c]8  }t        j                  |j                  |j                  |j                  d      : c}      }|j                  j                         D ch c]  }|j                  s|j                   }}t        j                  ||d| j                        S c c}w c c}w )z:Generates an unconstrained FunctionSpec from FunctionType.NF)r&   )r,   
isinstancewrap_function_libWrappedFunctionr   function_type_libFunctionType
parametersvalues	Parameterr&   kindoptionaldefaultr   r   )r0   r   punconstrained_typedefault_valuess        r    get_preinitialized_function_specrD   Y   s    
 119Z*::> #11-(55 !++224 
%
%affaffajj$
G '1188:ajjaii.  
	)	)!!	
 
s   "=DDDc                     t        j                  | j                  | j                  | j                        }t        |       }|$|j                  j                  t        |             |S )z"Build a SavedBareConcreteFunction.)concrete_function_nameallowed_positional_argumentsargument_keywords)	r   SavedBareConcreteFunctionr&   _num_positional_args_arg_keywordsrD   r   r   r    )r0   r   r   s      r    serialize_bare_concrete_functionrL   x   sa     !
:
:.33#4#I#I)779% 33DE-	  !9-!HI	,r!   c                     t        j                         }t        | j                        }|j                  j	                  |       |D ]'  }|j
                  j                  |j                         ) |S )zBuild a SavedFunction proto.)r   SavedFunctionr    r   r   concrete_functionsr$   r&   )r   rO   r   function_spec_protor0   s        r   serialize_functionrQ      sf    
 
.
.
0%01G1GH23- <	##$5$:$:;<	,r!   c                    t        j                  dj                  | j                  j                              }d}i }|j                         5  | j                  j                  D ]  \  }}t        |dd      }| |       }|j                         }t        |      }| j                  j                  j                  |   }	| j                  j                  j                  |   }
|	|
g||<   | j                  j                  j                  |||d       d} 	 ddd       |s| S t        j                  j!                  | j                  | j"                  i       fd}t%        j&                  | j(                  d      }t        j*                  d|t-        |      i |	       t        j                  j!                  || j"                  i       }| j.                  |_        | j0                  |_        |j3                         D ]3  \  }}|\  }	}
| j                  j4                  j                  ||	|
d       5 |S # 1 sw Y   !xY w)
a  Wraps the concrete function if it uses cached read tensors.

  This function creates a new concrete function that captures variables
  instead of the cached read tensors.

  Args:
    concrete_function: A Concrete function that maybe captures cached read
      tensors.

  Returns:
    A concrete function that wraps the original concrete function, which
    captures variables instead. If the original function did not capture any
    cached values, then the function is not wrapped and the original object is
    returned.
  z{}_no_cacheN_cached_variableF)keyexternalinternal	is_by_refTc                  N    j                  t        |       j                        S )N)
_call_flatlistr#   )r   inner_concretes    r   r   z,wrap_cached_variables.<locals>.wrap_function   s     $$T$Z1O1OPPr!   )expand_composites)r   kwargsr   )r(   	FuncGraphformatgraphr&   
as_defaultcapturesgetattr
read_valueidfunction_capturesby_val_externalby_val_internaladd_or_replacedefunConcreteFunctionfrom_func_graphr   r
   flattenr,   func_graph_from_py_functuplerK   rJ   items_function_captures)r0   outer_graphmapped_capturesremapped_capturesr2   placeholdercached_variablenew_cached_valuerT   rU   rV   r   r   fnr[   s                 @r   wrap_cached_variablesry      sD     "++,22778:+/   1 7 7 @ @ );TBo		 ')o(335wKc"((::JJ3Oh"((::JJ3Oh ((3//>>#	 ? 
 o$ 
))990>>.Q 
'BB(,
.$++
MdB --$22B" '44"-BB" (--/ lc7 Hh..==	 >  
)i s   C	H77IN)__doc__%tensorflow.core.function.polymorphismr   r9   tensorflow.core.protobufr   tensorflow.python.eagerr   rj   r   r7   ,tensorflow.python.eager.polymorphic_functionr   tensorflow.python.frameworkr   r(   tensorflow.python.saved_modelr	   tensorflow.python.utilr
   r    r4   rD   rL   rQ   ry    r!   r   <module>r      sB    ) T ; 5 F L G @ ' F!6>
Kr!   