o
    ey-                     @   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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
S )*zCFunctions called by the generated code to execute an eager-mode op.    )text_format)
tensor_pb2)
pywrap_tfe)core)dtypes)ops)tensor_shape)compatNc           
   
   C   s   |j }z|  t|j|| |||}W |S  tjy5 } z|dur+| jd| 7  _t|dd}~w t	yS } zdd |D }	|	rMt
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.
  N name: c                 S      g | ]	}t |r|qS  r   Z_is_keras_symbolic_tensor.0xr   r   OD:\Projects\ConvertPro\env\Lib\site-packages\tensorflow/python/eager/execute.py
<listcomp>;   
    
z!quick_execute.<locals>.<listcomp>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   s0   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   q   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_cancellationG   s4   
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   
r,   c                   C      dS )/Import backprop if you want gradients recorded.Fr   r   r   r   r   must_record_gradient   s   r/   c                 C   r-   )r.   Nr   )Zunused_op_nameZunused_inputsZunused_attrsZunused_outputsr   r   r   record_gradient   s   r0   c                 C   s(   t | tjstd|t| f t| S )Nz(Expected float for argument '%s' not %s.)
isinstancer	   Z
real_typesr   reprfloatvarg_namer   r   r   
make_float   s
   
r7   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.)r1   strr   r2   int
ValueErrorr4   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.)r1   r	   Zbytes_or_text_typesr   r2   as_bytesr4   r   r   r   make_str   s
   

r=   c                 C   s"   t | tstd|t| f | S )Nz'Expected bool for argument '%s' not %s.)r1   boolr   r2   r4   r   r   r   	make_bool   s
   

r?   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   r2   as_datatype_enum)r5   r6   ir   r   r   	make_type   s   
rB   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)r5   r6   shaper%   r   r   r   
make_shape   s   	
rE   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'.)r1   r   ZTensorProtor8   r   ZMerger   r2   )r5   r6   Zpbr   r   r   make_tensor   s   

rF   c           
         s  | s
|dur
|g fS t j}| D ]	}t||s n	q| d  | fS d| D ]}t||r1|j nq%du rmg }| D ]1}d}du rR|rRt j| d}|j|vrRd}|du r_t j|| d}|| du rk|jq:n
 fdd| D }dd |D }	|	rtd	|	j
|fS )	z0Convert sequence `l` to eager same-type Tensors.Nr   r"   )Zpreferred_dtyper"   c                    s   g | ]
}t j| d qS rG   r   convert_to_tensorr   tr"   dtyper   r   r     s    z*args_to_matching_eager.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   r         z@Using symbolic output of a Keras layer during eager execution {})r   EagerTensorr1   _datatype_enumrN   rJ   appendr   r   r   r@   )
lr"   Zallowed_dtypesZdefault_dtyperP   r   rL   retZtensorr&   r   rM   r   args_to_matching_eager   sN   




rU   c                    s(    fdd| D }dd |D }||fS )Nc                    s   g | ]	}t j| d qS rH   rI   rK   rG   r   r   r     rO   z2convert_to_mixed_eager_tensors.<locals>.<listcomp>c                 S   s   g | ]}|  qS r   )rQ   rK   r   r   r   r     s    r   )valuesr"   r5   typesr   rG   r   convert_to_mixed_eager_tensors  s   rX   c              	   C   sf  t | dksJ g g}| dd D ]#}t |t | d kr/tdt | d t || d |f |g  qg }tt | d D ]o}d}| D ]}t|| tjrV|| j} nqE|du r|d tj| d | |d |d | j}tdt | D ]}|| tj| | | ||d qyn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).rG   )rN   r"   )
lenr:   rR   ranger1   r   rP   rN   rJ   r@   )listsr"   Z	lists_retrS   rW   rA   rN   jr   r   r   args_to_mixed_eager_tensors   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.utilr	   r'   r)   r,   executer/   r0   r7   r;   r=   r?   rB   rE   rF   rU   rX   r^   r   r   r   r   <module>   s4   
2

6	

9