o
    ?e;"                     @   s   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 Zdd Zdd Zdd Zdd Zdd ZdS )z"Tools for serializing `Function`s.    )function_type)saved_object_graph_pb2)function)wrap_function)function_type_utils)
func_graph)nested_structure_coder)nestc                 C   s   | j jr| j jd dkrtdt }|j t| j ji d d|_	|j
t| j
 tjjjtjjjtjjjd| j|_|S )z>Serialize a FunctionSpec object into its proto representation.r   selfz<Can not serialize tf.function with unbound 'self' parameter.)annotationsF)NTF)Zfullargspecargs	TypeErrorr   FunctionSpecCopyFromr   encode_structure_replaceZ	is_methodZinput_signatureZ
JitCompileDEFAULTONZOFFgetZjit_compile)function_specproto r   u/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/saved_model/function_serialization.py_serialize_function_spec   s0   
r   c              	   C   s   g }z| j D ]	}|||  qW n ty$   td| j d|dw t }t| j}|j	
t| j |j
t| |j| |S )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   ZSavedConcreteFunctionfunc_graph_moduleZconvert_structure_to_signaturestructured_outputsZcanonicalized_input_signaturer   r   r   structured_input_signatureZoutput_signaturebound_inputsextend)concrete_functionZnode_idsr!   captureZconcrete_function_protor   r   r   r   serialize_concrete_function>   s4   

r%   c                 C   sn   | j du st| tjrdS | j}|du rdS tdd |j D }dd |j D }t	j
||d| jdS )z:Generates an unconstrained FunctionSpec from FunctionType.Nc                 S   s"   g | ]}t |j|j|jd qS N)function_type_lib	Parameterr   kindoptional.0pr   r   r   
<listcomp>h   s    z4get_preinitialized_function_spec.<locals>.<listcomp>c                 S   s   h | ]}|j r|jqS r   )r*   defaultr+   r   r   r   	<setcomp>m   s
    z3get_preinitialized_function_spec.<locals>.<setcomp>F)r   )r    
isinstancewrap_function_libZWrappedFunctionr   r'   FunctionType
parametersvaluesr   r   r   )r#   r   Zunconstrained_typedefault_valuesr   r   r    get_preinitialized_function_specY   s*   r7   c                 C   s:   t j| j| j| jd}t| }|dur|jt| |S )z"Build a SavedBareConcreteFunction.)Zconcrete_function_nameZallowed_positional_argumentsZargument_keywordsN)	r   ZSavedBareConcreteFunctionr   _num_positional_args_arg_keywordsr7   r   r   r   )r#   r   r   r   r   r    serialize_bare_concrete_functionx   s   r:   c                 C   s:   t  }t| j}|j| |D ]	}|j|j q|S )zBuild a SavedFunction proto.)r   ZSavedFunctionr   r   r   concrete_functionsr   r   )r   r;   r   Zfunction_spec_protor#   r   r   r   serialize_function   s   
r<   c                    sj  t d| jj}d}i }| H | jjD ];\}}t|dd}|du r&q| }| }t	|}| jj
j| }	| jj
j| }
|	|
g||< | jj
j|||dd d}qW d   n1 s]w   Y  |sf| S tj| j| ji   fdd}tj| jdd	}t jd|t|i |d
 tj|| ji }| j|_| j|_| D ]\}}|\}	}
| jjj||	|
dd q|S )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_cacheNZ_cached_variableF)keyexternalinternalZ	is_by_refTc                     s     t|  jS r&   )Z
_call_flatlistr   )r   Zinner_concreter   r   r      s   z,wrap_cached_variables.<locals>.wrap_function)Zexpand_composites)r   kwargsr   )r   Z	FuncGraphformatgraphr   Z
as_defaultZcapturesgetattrZ
read_valueidZfunction_capturesZby_val_externalZby_val_internalZadd_or_replacedefunZConcreteFunctionZfrom_func_graphr   r	   flattenr    Zfunc_graph_from_py_functupler9   r8   itemsZ_function_captures)r#   Zouter_graphZmapped_capturesZremapped_capturesr$   placeholderZcached_variableZnew_cached_valuer=   r>   r?   r   r   fnr   rA   r   wrap_cached_variables   sh   

rM   N)__doc__Z%tensorflow.core.function.polymorphismr   r'   Ztensorflow.core.protobufr   Ztensorflow.python.eagerr   rG   r   r2   Z,tensorflow.python.eager.polymorphic_functionr   Ztensorflow.python.frameworkr   r   Ztensorflow.python.saved_modelr   Ztensorflow.python.utilr	   r   r%   r7   r:   r<   rM   r   r   r   r   <module>   s   #