
    BVh6<                        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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!d Z"d Z#d Z$ G d dejJ                  ejL                        Z' ejP                  dd  ejR                  d d d d e'jT                  !      g"       d# Z+d$ Z,y%)&z;Helpers for working with signatures in tf.saved_model.save.    )logging)def_functionfunction)
attributes)composite_tensor)tensor)resource_variable_ops)function_serialization)revived_types)signature_constants)base)core)compat)nest)collections_abc_default_save_signature
signatures   c                     t        | t        j                        r| j                  | j	                         } t        | t
        j                        sy | S N)
isinstancer   Functioninput_signature(_get_concrete_function_garbage_collecteddefunConcreteFunctionr   s    e/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/saved_model/signature_serialization.py_get_signaturer   (   sD    <001

"
"
.@@BH	He44	5	/    c                     | j                   sy	 t        |        t        | j                  dd       y# t        $ r Y yw xY w)zBReturns whether concrete function can be converted to a signature.FunusedT)outputs_validate_inputs_normalize_outputsstructured_outputs
ValueErrorconcrete_functions    r   _valid_signaturer*   3   sJ    		"	" &'(;;XxP 
 
 s   "2 	>>c                     t        d t        j                  | j                        D              rt	        d| j
                   d      y)z*Raises error if input type is tf.Variable.c              3   P   K   | ]  }t        |t        j                           y wr   )r   r
   VariableSpec).0inps     r   	<genexpr>z#_validate_inputs.<locals>.<genexpr>D   s&      	
 +889	s   $&z'Unable to serialize concrete_function 'zc'with tf.Variable input. Functions that expect tf.Variable inputs cannot be exported as signatures.N)anyr   flattenstructured_input_signaturer'   namer(   s    r   r$   r$   B   sU     	/JJK	  
12C2H2H1I J3 	3 	r    c                 N   i }t        | j                  j                  j                  | j                  j
                        D ]R  \  }}	 t        j                  |j                  j                  d            }|j                  |k7  r|j                  ||<   T |S # t        $ r Y bw xY w)zDChecks for user-specified signature input names that are normalized._user_specified_name)zipfunction_def	signature	input_arggraphinputsr   as_stropget_attrr4   r'   )r)   name_changessignature_input_namegraph_inputuser_specified_names        r   _get_signature_name_changesrD   O   s     ,+.$$..88$$, 'K"MM
..
!
!"8
9 
	"	"&9	9,@,E,E() 
  
s   AB	B$#B$c                 r   | j                  | j                        }g }|D ]h  \  }}t        |t        j                  t
        j                  f      s1|t        k(  r|c S t        |      }|Lt        |      sX|j                  |       j t        |      dk(  r|d   }t        |      }|rt        |      r|S y)z;Function to export, None if no suitable function was found.N   r   )list_childrenrootr   r   r   r   r   DEFAULT_SIGNATURE_ATTRr   r*   appendlen)saveable_viewchildrenpossible_signaturesr4   childconcretesingle_functionr9   s           r   find_function_to_exportrR   c   s     ((););<(
  +kdEel33U5K5KLM%%le$H 0 :  *+ 		")!,O/I%i0	r    c           
         | i i i fS t        | t        j                        st        j                  | i} d}i }i }i }| j                         D ]y  \  }t        |      x}t        d| d      |j                  |      xs t        j                  |      x||<   t               |t        k  rt              }|r~|dz  }t        j                  dt!        j"                  j$                  j&                        dj)                  |j+                               dj)                  |j-                                      fd}t/        |d	      rd
|j0                  z   |_        i }	t2        j4                  D ]D  }
j6                  j8                  j                  |
d      }|
t2        j:                  k7  s=|@||	|
<   F |	sd}	t=        j>                  ||	      }i }j@                  -tC        d tE        jF                  j@                  d            }njH                  }tK        jL                  |      D ]  \  }}t!        j"                  |      }t        |tN        jP                        r-tO        jP                  |jR                  |jT                  |      }n!tN        jP                  jW                  ||      }|||<     |jX                  di |}t[        |jL                        dk(  rd|_.        nd|_.        ||<   t        |t^        j`                        stE        jF                  |jb                  jd                  jf                        }t[        |xs g       }ti        |j+                               }|dkD  s.tK        || d |xs g       D ]5  \  }}t        |tN        jj                        s!||jm                  i       |<   7 | |||fS )z>Converts `signatures` into a dictionary of concrete functions.Nr   zJExpected a TensorFlow function for which to generate a signature, but got z_. Only `tf.functions` with an input signature or concrete functions can be used as a signature.rF   zrFunction `%s` contains input name(s) %s with unsupported characters which will be renamed to %s in the SavedModel.z, c                  B     di | }t        |j                        S )N )r%   r4   )kwargsr&   signature_functionsignature_keys     r   signature_wrapperz2canonicalize_signatures.<locals>.signature_wrapper   s,    -77
055} r    __name__signature_wrapper_)experimental_attributesc                 6    t        | t        j                        S r   )r   r	   
TensorSpec)xs    r   <lambda>z)canonicalize_signatures.<locals>.<lambda>   s    Jq&"3"34 r    T)expand_composites)r4   rU   )7r   r   Mappingr   !DEFAULT_SERVING_SIGNATURE_DEF_KEYitemsr   r'   getr   wrap_cached_variablesr$   "_NUM_DISPLAY_NORMALIZED_SIGNATURESrD   r   infor   r=   r;   r4   joinkeysvalueshasattrrZ   r   POLYMORPHIC_FUNCTION_ALLOWLISTr8   attr	NO_INLINEr   r   r3   filterr   r2   r<   r7   _arg_keywordsr	   r^   shapedtypefrom_tensorr   rK   _num_positional_argsr   PolymorphicFunctionfunction_specfullargspecdefaultslistTensor
setdefault)r   !num_normalized_signatures_counterconcrete_signatureswrapped_functionsry   r   original_functionsignature_name_changesrY   r\   rn   
attr_valuewrapped_functiontensor_spec_signaturer<   keywordr/   specfinal_concreteflattened_defaultslen_default	arg_namesargdefaultrW   rX   s                           @@r   canonicalize_signaturesr   ~   s   r2:	J 7 7	8==zJ '(#(!+!1!1!3 j@mX-;H-EE*!J ;;  	/0 	K!778IJ'(+= '((+MM:;MN	)Q.)HMM,22778II,1134II,3356	
 x$#7(:K:K#K 
 !99 3%2277;;D$Gj	%%	%**@(2%3 # $#,,3J 44@
4
,, ;; $f "((f(( 	, g&g	C**	+  CIIGD  ,,Sw,?'+G$	, O%NN 
N >''(A-
 -.n),-n))7&(D445<<

 
 
,
,
5
5 *0b1k,1134i	q  {lm$$"
 	@LC GV]]38?(

mR
0
5	@Ij@V 
/	99r    c                 r   t        | t        j                        sgt        | d      r| j	                         } nJt        | t        j
                        s| g} t        |       D ci c]  \  }}dj                  |      | } }}| j                         D ]  \  }}t        |t        j                        s(t        d|dt        j                  |       d|d      t        |t        j                  t        j                   f      rst        d|d|dt        j                  |       d|d		       | S c c}}w )
z?Normalize outputs if necessary and check that they are tensors._asdictz	output_{}z'Got a dictionary with a non-string key z in the output of the function z+ used to generate the SavedModel signature .zGot a non-Tensor value z	 for key z. Outputs for functions used as signatures must be a single Tensor, a sequence of Tensors, or a dictionary from string to Tensor.)r   r   rb   rl   r   Sequence	enumerateformatrd   r   bytes_or_text_typesr'   
as_str_anyr	   r{   r   CompositeTensor)r#   function_namerX   output_indexoutputkeyvalues          r   r%   r%      sO    
G_44	5w	"!g!9!9:) '0&8"lF 

\
*F
2g  MMO jc5c65563C7 ; ++M:; <&&3%6a9 
 efmm-=-M-MNO#E9IcW = ++M:; <&&3%6 7JJ  
.+s   #D3c                   f    e Zd ZdZd Zd Zd Zd Zd Zd Z	e
j                  j                  fdZy	)
_SignatureMapz&A collection of SavedModel signatures.c                     i | _         y r   _signaturesselfs    r   __init__z_SignatureMap.__init__$  s
    Dr    c                 "    || j                   |<   y)z&Adds a signature to the _SignatureMap.Nr   )r   r4   r)   s      r   _add_signaturez_SignatureMap._add_signature'  s     /DTr    c                      | j                   |   S r   r   )r   r   s     r   __getitem__z_SignatureMap.__getitem__-  s    C  r    c                 ,    t        | j                        S r   )iterr   r   s    r   __iter__z_SignatureMap.__iter__0  s      !!r    c                 ,    t        | j                        S r   )rK   r   r   s    r   __len__z_SignatureMap.__len__3  s    t  r    c                 8    dj                  | j                        S )Nz_SignatureMap({}))r   r   r   s    r   __repr__z_SignatureMap.__repr__6  s    %%d&6&677r    c           	          |t         j                  j                  k7  ri S | j                         D ci c]2  \  }}t	        |t
        j                  t        j                  f      r||4 c}}S c c}}w r   )	r   SaveType
SAVEDMODELrd   r   r   r   r   r   )r   	save_typerV   r   r   s        r   _trackable_childrenz!_SignatureMap._trackable_children9  sd    DMM,,,i **,Cel33U5K5KLM 	U
  s   7A.N)rZ   
__module____qualname____doc__r   r   r   r   r   r   r   r   
CHECKPOINTr   rU   r    r   r   r   !  s7    ./!"!8 +/--*B*B r    r   signature_mapc                 "    t        | t              S r   )r   r   )objs    r   r`   r`   F  s    
3. r    c                     t               S r   )r   )protos    r   r`   r`   L  s     r    rF   )object_factoryversionmin_producer_versionmin_consumer_versionsetter)versionsc                 f   t               }| j                         D ]  \  }}t        |t        j                        sJ t        |j
                  t        j                        sJ t        |j                        dk(  rd|j                  k(  sJ d|j                  k(  sJ |j                  ||        |S )z*Creates an object containing `signatures`.rF   r   )r   rd   r   r   r   r&   r   rb   rK   rq   ru   r   )r   r   r4   funcs       r   create_signature_mapr   V  s    /-$$& -jdD
 dE22333d--/F/FGGG
4!#$+++++$+++++  t,- 
r    c           
          | j                  | j                        D ]G  \  }}|t        k(  st        |t              s't        d| j                   dt         dt         d       y y)zCPerforms signature-related sanity checks on `augmented_graph_view`.zExporting an object z which has an attribute named 'z'. This is a reserved attribute used to store SavedModel signatures in objects which come from `tf.saved_model.load`. Delete this attribute (e.g. `del obj.z1`) before saving if this shadowing is acceptable.N)rG   rH   SIGNATURE_ATTRIBUTE_NAMEr   r   r'   )augmented_graph_viewr4   deps      r   validate_augmented_graph_viewr   j  s    '55 idC '']+"#7#<#<"= >!!9 : ; 22 3(	(
 	
 r    N)-r   abslr   tensorflow.python.eagerr   r   r   ,tensorflow.python.eager.polymorphic_functionr   tensorflow.python.frameworkr   r	   tensorflow.python.opsr
   tensorflow.python.saved_modelr   r   r   tensorflow.python.trackabler   tensorflow.python.typesr   tensorflow.python.utilr   r   tensorflow.python.util.compatr   rI   r   rg   r   r*   r$   rD   rR   r   r%   rb   	Trackabler   register_revived_typeVersionedTypeRegistrationr   r   r   rU   r    r   <module>r      s    B  0 5 C 8 . 7 @ 7 = , ( ) ' 92 ' %& "
(6w:tR O++T^^  F $ # #./// 9!"!" //		
$(r    