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	lmZ d+ddZ	
d+ddZd+ddZeZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd+d#d$Zd%d& Zd'd( Z d)d* Z!d
S ),zCFunctions called by the generated code to execute an eager-mode op.    )text_format)
tensor_pb2)
pywrap_tfe)core)dtypes)tensor_conversion_registry)tensor_shape)compatNc           
   
   C   s   |j }z|  dd |D }t|j|| |||}W |S  tjy< } z|dur2| jd| 7  _t|dd}~w t	yZ } zdd |D }	|	rTt
d|	|d}~ww )a  Execute a TensorFlow operation.

  Args:
    op_name: Name of the TensorFlow operation (see REGISTER_OP in C++ code) to
      execute.
    num_outputs: The number of outputs of the operation to fetch. (Explicitly
      provided instead of being inferred for performance reasons).
    inputs: A list of inputs to the operation. Each entry should be a Tensor, or
      a value which can be passed to the Tensor constructor to create one.
    attrs: A tuple with alternating string attr names and attr values for this
      operation.
    ctx: The value of context.context().
    name: Customized name for the operation.

  Returns:
    List of output Tensor objects. The list is empty if there are no outputs

  Raises:
    An exception on error.
  c                 S   s&   g | ]}t |tjrt|n|qS  )
isinstance
core_typesZTensorr   convert.0tr
   r
   `/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/eager/execute.py
<listcomp>6   s    
z!quick_execute.<locals>.<listcomp>N name: c                 S      g | ]}t |r|qS r
   _is_keras_symbolic_tensorr   xr
   r
   r   r   C       QInputs to eager execution function cannot be Keras symbolic tensors, but found {})device_nameensure_initializedr   ZTFE_Py_Execute_handler   _NotOkStatusExceptionmessage_status_to_exception	TypeError_SymbolicExceptionformat)
op_namenum_outputsinputsattrsctxnamer   tensorsekeras_symbolic_tensorsr
   r
   r   quick_execute   s2   r-   c              
   C   s   |j }z|  t|j|| |||j|}W |S  tjy7 }	 z|dur-|	 jd| 7  _t	|	dd}	~	w t
yU }	 zdd |D }
|
rOtd|
|	d}	~	ww )aT  Execute a TensorFlow operation.

  Args:
    op_name: Name of the TensorFlow operation (see REGISTER_OP in C++ code) to
      execute.
    num_outputs: The number of outputs of the operation to fetch. (Explicitly
      provided instead of being inferred for performance reasons).
    inputs: A list of inputs to the operation. Each entry should be a Tensor, or
      a value which can be passed to the Tensor constructor to create one.
    attrs: A tuple with alternating string attr names and attr values for this
      operation.
    ctx: The value of context.context().
    cancellation_manager: a `CancellationManager` object that can be used to
      cancel the operation.
    name: Customized name for the operation.

  Returns:
    List of output Tensor objects. The list is empty if there are no outputs

  Raises:
    An exception on error.
  Nr   c                 S   r   r
   r   r   r
   r
   r   r   w   r   z-execute_with_cancellation.<locals>.<listcomp>r   )r   r   r   ZTFE_Py_ExecuteCancelabler   _implr   r   r   r    r!   r"   r#   )r$   r%   r&   r'   r(   Zcancellation_managerr)   r   r*   r+   r,   r
   r
   r   execute_with_cancellationM   s0   
r/   c                 C   s6   t | |||||}|jD ]}|| t|||| q|S )z6Monkey-patch to execute to enable execution callbacks.)r-   Zop_callbackstuple)r$   r%   r&   r'   r(   r)   r*   callbackr
   r
   r   execute_with_callbacks   s   
r2   c                   C      dS )/Import backprop if you want gradients recorded.Fr
   r
   r
   r
   r   must_record_gradient   s   r5   c                 C   r3   )r4   Nr
   )Zunused_op_nameZunused_inputsZunused_attrsZunused_outputsr
   r
   r   record_gradient   s   r6   c                 C   s(   t | tjstd|t| f t| S )Nz(Expected float for argument '%s' not %s.)r   r	   Z
real_typesr!   reprfloatvarg_namer
   r
   r   
make_float   s
   
r<   c              	   C   sP   t | trtd|t| f zt| W S  ttfy'   td|t| f w )Nz&Expected int for argument '%s' not %s.)r   strr!   r7   int
ValueErrorr9   r
   r
   r   make_int   s   



r@   c                 C   s*   t | tjstd|t| f t| S )Nz)Expected string for argument '%s' not %s.)r   r	   Zbytes_or_text_typesr!   r7   as_bytesr9   r
   r
   r   make_str   s
   

rB   c                 C   s"   t | tstd|t| f | S )Nz'Expected bool for argument '%s' not %s.)r   boolr!   r7   r9   r
   r
   r   	make_bool   s
   

rD   c                 C   s>   zt | j} W n ty   td|t| f w | j}|S )Nz+Expected DataType for argument '%s' not %s.)r   Zas_dtypeZ
base_dtyper!   r7   as_datatype_enum)r:   r;   ir
   r
   r   	make_type   s   
rG   c              
   C   st   zt | }W n' ty } ztd||f d}~w ty. } ztd||f d}~ww |jdu r6dS | S )zConvert v into a list.z)Error converting %s to a TensorShape: %s.N)r   Zas_shaper!   r?   Zndimsas_list)r:   r;   shaper+   r
   r
   r   
make_shape   s   	
rJ   c                 C   sF   t | tjr| S t | trt }t| | |S tdt| |f )zEnsure v is a TensorProto.z@Don't know how to convert %s to a TensorProto for argument '%s'.)r   r   ZTensorProtor=   r   ZMerger!   r7   )r:   r;   Zpbr
   r
   r   make_tensor   s   

rK   c           	         s  ~| s|dur|g fS | D ]
}t |tjs q q| d  | fS d | D ]}t |tjr1|j  q2q$ du rjg }| D ].}d} du rP|rPt|}|j|vrPd}|du r\tj| |d}||  du rh|j q:n	 fdd| D }dd |D }|rt	d
| j|fS )z0Convert sequence `l` to eager same-type Tensors.Nr   )Zpreferred_dtypec                    s   g | ]}t | qS r
   r   r   r   dtyper
   r   r     r   z*args_to_matching_eager.<locals>.<listcomp>c                 S   r   r
   r   r   r
   r
   r   r     r   z@Using symbolic output of a Keras layer during eager execution {})r   r   Value_datatype_enumrN   r   r   appendr   r"   r#   rE   )	lr(   Zallowed_dtypesZdefault_dtyper   r   retZtensorr,   r
   rM   r   args_to_matching_eager   sN   



rT   c                 C   s&   ~dd | D }dd |D }||fS )Nc                 S   s   g | ]}t |qS r
   rL   r   r
   r
   r   r   !  s    z2convert_to_mixed_eager_tensors.<locals>.<listcomp>c                 S   s   g | ]}|  qS r
   )rP   r   r
   r
   r   r   "  s    r
   )valuesr(   r:   typesr
   r
   r   convert_to_mixed_eager_tensors  s   rW   c                 C   s`  ~t | dks	J g g}| dd D ]#}t |t | d kr0tdt | d t || d |f |g  qg }tt | d D ]k}d}| D ]}t|| tjrW|| j} qXqF|du r|d t	| d |  |d | j}tdt | D ]}|| tj	| | | |d qxntt | D ]}|| tj	| | | |d q||j
 q@||fS )z@Converts a list of same-length lists of values to eager tensors.   Nr   zDExpected list arguments to be the same length: %d != %d (%r vs. %r).rM   )lenr?   rQ   ranger   r   rO   rN   r   r   rE   )listsr(   Z	lists_retrR   rV   rF   rN   jr
   r
   r   args_to_mixed_eager_tensors&  sB   
r]   c                 C   s   t | dot| jdd dkS )Ngraphr)   Zkeras_graph)hasattrgetattrr^   )r   r
   r
   r   r   O  s   r   )N)"__doc__Zgoogle.protobufr   Ztensorflow.core.frameworkr   Ztensorflow.pythonr   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   r   r   Ztensorflow.python.typesr   Ztensorflow.python.utilr	   r-   r/   r2   executer5   r6   r<   r@   rB   rD   rG   rJ   rK   rT   rW   r]   r   r
   r
   r
   r   <module>   s8   
7

4	

:)