a
    /Sic\,                     @   s   d Z ddl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.    N)text_format)
tensor_pb2)
pywrap_tfe)core)dtypes)ops)tensor_shape)compatc           
   
   C   s   |j }z"|  t|j|| |||}W n tjyt } z2|durT| jd| 7  _t|dW Y d}~nRd}~0  t	y } z2dd |D }	|	rt
d|	|W Y d}~n
d}~0 0 |S )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   s   g | ]}t |r|qS  r   Z_is_keras_symbolic_tensor.0xr   r   [/var/www/html/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/eager/execute.py
<listcomp>=   s   z!quick_execute.<locals>.<listcomp>QInputs to eager execution function cannot be Keras symbolic tensors, but found {})device_nameensure_initializedr   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   s,     r&   c              
   C   s   |j }z&|  t|j|| |||j|}W n tjyx }	 z2|durX|	 jd| 7  _t	|	dW Y d}	~	nRd}	~	0  t
y }	 z2dd |D }
|
rtd|
|	W Y d}	~	n
d}	~	0 0 |S )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   s   g | ]}t |r|qS r   r   r   r   r   r   r   s   s   z-execute_with_cancellation.<locals>.<listcomp>r   )r   r   r   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_cancellationI   s0    
 r)   c                 C   s6   t | |||||}|jD ]}|| t|||| q|S )z6Monkey-patch to execute to enable execution callbacks.)r&   op_callbackstuple)r   r   r   r    r!   r"   r#   callbackr   r   r   execute_with_callbacks   s    
r-   c                   C   s   dS )/Import backprop if you want gradients recorded.Fr   r   r   r   r   must_record_gradient   s    r/   c                 C   s   dS )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	   
real_typesr   reprfloatvarg_namer   r   r   
make_float   s
    
r8   c              	   C   sZ   t | tjr td|t| f z
t| W S  ttfyT   td|t| f Y n0 d S )Nz&Expected int for argument '%s' not %s.)r1   sixstring_typesr   r3   int
ValueErrorr5   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	   bytes_or_text_typesr   r3   as_bytesr5   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   r3   r5   r   r   r   	make_bool   s
    

rB   c                 C   sB   zt | j} W n& ty6   td|t| f Y n0 | j}|S )Nz+Expected DataType for argument '%s' not %s.)r   as_dtypeZ
base_dtyper   r3   as_datatype_enum)r6   r7   ir   r   r   	make_type   s    
rF   c              
   C   s   zt | }W nf tyB } ztd||f W Y d}~n<d}~0  tyt } ztd||f W Y d}~n
d}~0 0 |jdu rdS | S dS )zConvert v into a list.z)Error converting %s to a TensorShape: %s.N)r   as_shaper   r<   ndimsas_list)r6   r7   shaper$   r   r   r   
make_shape   s    	$
rK   c                 C   sL   t | tjr| S t | tjr4t }t| | |S tdt| |f dS )zEnsure v is a TensorProto.z@Don't know how to convert %s to a TensorProto for argument '%s'.N)	r1   r   TensorProtor9   r:   r   ZMerger   r3   )r6   r7   Zpbr   r   r   make_tensor   s    
rM   c           
         s  | s|dur|g fS t j}| D ]}t||s qBq| d  | fS d| D ]}t||rJ|j qdqJdu rg }| D ]b}d}du r|rt j| d}|j|vrd}|du rt j|| d}|| du rt|jqtn fdd| D }dd |D }	|	rt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 rN   r   convert_to_tensorr   tr!   dtyper   r   r         z*args_to_matching_eager.<locals>.<listcomp>c                 S   s   g | ]}t |r|qS r   r   r   r   r   r   r     rV   z@Using symbolic output of a Keras layer during eager execution {})r   EagerTensorr1   _datatype_enumrU   rQ   appendr   r   r   rD   )
lr!   Zallowed_dtypesZdefault_dtyperW   r   rS   rettensorr%   r   rT   r   args_to_matching_eager   sH    




r]   c                    s(    fdd| D }dd |D }||fS )Nc                    s   g | ]}t j| d qS rO   rP   rR   rN   r   r   r     rV   z2convert_to_mixed_eager_tensors.<locals>.<listcomp>c                 S   s   g | ]}|  qS r   )rX   rR   r   r   r   r     rV   r   )valuesr!   r6   typesr   rN   r   convert_to_mixed_eager_tensors  s    r`   c              	   C   sj  t | dksJ g g}| dd D ]F}t |t | d kr^tdt | d t || d |f |g  q"g }tt | d D ]}d}| D ]"}t|| tjr|| j} qq|du r|d tj| d | |d |d | j}tdt | D ]&}|| tj| | | ||d qn6t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).rN   )rU   r!   )
lenr<   rY   ranger1   r   rW   rU   rQ   rD   )listsr!   Z	lists_retrZ   r_   rE   rU   jr   r   r   args_to_mixed_eager_tensors"  s<    

 rf   )N)N)N)N) __doc__r9   google.protobufr   tensorflow.core.frameworkr   tensorflow.pythonr   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   tensorflow.python.utilr	   r&   r)   r-   executer/   r0   r8   r=   r@   rB   rF   rK   rM   r]   r`   rf   r   r   r   r   <module>   s4   
2 
6
	

9