o
    i e2<                  
   @   sR  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d Zdd Zdd Z dd Z!dd Z"dd Z#d d! Z$G d"d# d#ej%ej&Z'ej(d$d%d& ej)d'd& d(d(d(e'j*d)gd* d+d, Z+d-d. Z,d/S )0z;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                 C   s2   t | tjr| jd ur|  } t | tjsd S | S N)
isinstancer   Functioninput_signature(_get_concrete_function_garbage_collecteddefunConcreteFunctionr    r   d/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/saved_model/signature_serialization.py_get_signature(   s   

r   c                 C   s<   | j sdS zt|  t| jdd W dS  ty   Y dS w )zBReturns whether concrete function can be converted to a signature.FunusedT)outputs_validate_inputs_normalize_outputsstructured_outputs
ValueErrorconcrete_functionr   r   r   _valid_signature3   s   r'   c                 C   s0   t dd t| jD rtd| j ddS )z*Raises error if input type is tf.Variable.c                 s   s    | ]	}t |tjV  qd S r   )r   r	   VariableSpec).0inpr   r   r   	<genexpr>D   s
    

z#_validate_inputs.<locals>.<genexpr>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%   r   r   r   r!   B   s   
r!   c              	   C   sb   i }t | jjj| jjD ]"\}}zt|j	d}|j
|kr$|j
||< W q ty.   Y qw |S )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_attrr/   r$   )r&   name_changessignature_input_namegraph_inputuser_specified_namer   r   r   _get_signature_name_changesO   s    


r>   c                 C   s   |  | j}g }|D ]'\}}t|tjtjfsq
|tkr |  S t|}|dur1t	|r1|
| q
t|dkrH|d }t|}|rHt	|rH|S dS )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_signaturesr/   childconcretesingle_functionr3   r   r   r   find_function_to_exportc   s"   
rK   c              
      s  | du r	i i i fS t | tjstj| i} d}i }i }i }|  D ],\}t| }  du r7td| d||p@t	
| ||<  t  |tk rnt }|rn|d7 }tdt jjd| d|   fdd	}t|d
rd|j |_i }	tjD ]}
 jj|
d}|
tjkr|dur||	|
< q|	sd}	tj||	d}i } j durt!dd t"j# j dd}n j$}t% j&|D ]&\}}t|}t |t'j(rt'j(|j)|j*|d}nt'j(j+||d}|||< q|j,di |}t-|j&dkrd|_.nd|_.||< t |t/j0rMt"#|j1j2j3}t-|pg }t4| }|dkrMt%|| d |p4g D ]\}}t |t'j5sCq6||6i |< q6q |||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.r?   zrFunction `%s` contains input name(s) %s with unsupported characters which will be renamed to %s in the SavedModel.z, c                     s    di | }t | jS )Nr   )r"   r/   )kwargsr#   signature_functionsignature_keyr   r   signature_wrapper   s   z2canonicalize_signatures.<locals>.signature_wrapper__name__signature_wrapper_)experimental_attributesc                 S   s   t | tjS r   )r   r   
TensorSpec)xr   r   r   <lambda>   s    z)canonicalize_signatures.<locals>.<lambda>T)expand_composites)r/   r   )7r   r   Mappingr   !DEFAULT_SERVING_SIGNATURE_DEF_KEYitemsr   r$   getr
   wrap_cached_variablesr!   "_NUM_DISPLAY_NORMALIZED_SIGNATURESr>   r   infor   r7   r5   r/   joinkeysvalueshasattrrQ   r   POLYMORPHIC_FUNCTION_ALLOWLISTr2   attr	NO_INLINEr   r   r.   filterr   r-   r6   r1   _arg_keywordsr   rT   shapedtypefrom_tensorr   rD   _num_positional_argsr   GenericFunctionfunction_specfullargspecdefaultslistTensor
setdefault)r   !num_normalized_signatures_counterconcrete_signatureswrapped_functionsro   r   original_functionsignature_name_changesrP   rS   rd   
attr_valuewrapped_functiontensor_spec_signaturer6   keywordr*   specfinal_concreteflattened_defaultslen_default	arg_namesargdefaultr   rM   r   canonicalize_signatures~   s   





	



r   c                 C   s   t | tjs"t| dr|  } nt | tjs| g} dd t| D } |  D ]8\}}t |tj	sAt
d|dt| d|dt |tjtjfs^t
d|d	|dt| d|d
	q&| S )z?Normalize outputs if necessary and check that they are tensors._asdictc                 S   s   i | ]
\}}d  ||qS )z	output_{})format)r)   output_indexoutputr   r   r   
<dictcomp>  s    
z&_normalize_outputs.<locals>.<dictcomp>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   rX   rb   r   Sequence	enumeraterZ   r   bytes_or_text_typesr$   
as_str_anyr   rq   r   CompositeTensor)r    function_namerO   keyvaluer   r   r   r"      s6   

r"   c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
jjfddZdS )_SignatureMapz&A collection of SavedModel signatures.c                 C   s
   i | _ d S r   _signaturesselfr   r   r   __init__$     
z_SignatureMap.__init__c                 C   s   || j |< dS )z&Adds a signature to the _SignatureMap.Nr   )r   r/   r&   r   r   r   _add_signature'  s   z_SignatureMap._add_signaturec                 C   s
   | j | S r   r   )r   r   r   r   r   __getitem__-  r   z_SignatureMap.__getitem__c                 C   
   t | jS r   )iterr   r   r   r   r   __iter__0  r   z_SignatureMap.__iter__c                 C   r   r   )rD   r   r   r   r   r   __len__3  r   z_SignatureMap.__len__c                 C   s   d | jS )Nz_SignatureMap({}))r   r   r   r   r   r   __repr__6  s   z_SignatureMap.__repr__c                 K   s"   |t jjkri S dd |  D S )Nc                 S   s(   i | ]\}}t |tjtjfr||qS r   )r   r   r   r   r   )r)   r   r   r   r   r   r   =  s    z5_SignatureMap._trackable_children.<locals>.<dictcomp>)r   SaveType
SAVEDMODELrZ   )r   	save_typerL   r   r   r   _trackable_children9  s
   z!_SignatureMap._trackable_childrenN)rQ   
__module____qualname____doc__r   r   r   r   r   r   r   r   
CHECKPOINTr   r   r   r   r   r   !  s    r   signature_mapc                 C   s
   t | tS r   )r   r   )objr   r   r   rV   F  s   
 rV   c                 C   s   t  S r   )r   )protor   r   r   rV   L  s    r?   )object_factoryversionmin_producer_versionmin_consumer_versionsetter)versionsc                 C   sv   t  }|  D ]1\}}t|tjsJ t|jtjsJ t|j	dkr+d|j
ks*J nd|j
ks2J ||| q|S )z*Creates an object containing `signatures`.r?   r   )r   rZ   r   r   r   r#   r   rX   rD   rg   rk   r   )r   r   r/   funcr   r   r   create_signature_mapV  s   r   c              	   C   sP   |  | jD ]\}}|tkr%t|ts"td| j dt dt d dS qdS )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)r@   rA   SIGNATURE_ATTRIBUTE_NAMEr   r   r$   )augmented_graph_viewr/   depr   r   r   validate_augmented_graph_viewj  s   

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   rB   r   r]   r   r'   r!   r>   rK   r   r"   rX   	Trackabler   register_revived_typeVersionedTypeRegistrationr   r   r   r   r   r   r   <module>   sR   z)#