o
    2h-                     @   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|  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  _is_keras_symbolic_tensor.0xr   r   Z/var/www/html/chatgem/venv/lib/python3.10/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   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 |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   p   r   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$   cancellation_managerr%   r   r&   r'   r(   r   r   r   execute_with_cancellationF   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_callbacksz   s   
r1   c                   C      dS )/Import backprop if you want gradients recorded.Fr   r   r   r   r   must_record_gradient   s   r4   c                 C   r2   )r3   Nr   )unused_op_nameunused_inputsunused_attrsunused_outputsr   r   r   record_gradient   s   r9   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
   
rA   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   r<   int
ValueErrorr>   r   r   r   make_int   s   



rE   c                 C   s*   t | tjstd|t| f t| S )Nz)Expected string for argument '%s' not %s.)r:   r	   bytes_or_text_typesr   r<   as_bytesr>   r   r   r   make_str   s
   

rH   c                 C   s"   t | tstd|t| f | S )Nz'Expected bool for argument '%s' not %s.)r:   boolr   r<   r>   r   r   r   	make_bool   s
   

rJ   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   as_dtype
base_dtyper   r<   as_datatype_enum)r?   r@   ir   r   r   	make_type   s   
rO   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   as_shaper   rD   ndimsas_list)r?   r@   shaper'   r   r   r   
make_shape   s   	
rT   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   TensorProtorB   r   Merger   r<   )r?   r@   pbr   r   r   make_tensor   s   

rX   c           	         s  ~| s|dur|g fS | D ]
}t |tjs n	q| d  | fS d | D ]}t |tjr1|j  nq$ 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   )preferred_dtypec                    s   g | ]}t | qS r   r   convertr   t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:   
core_typesValue_datatype_enumr_   r   r[   appendr   r   r   rM   )	lr$   allowed_dtypesdefault_dtyper   r]   rettensorr(   r   r^   r   args_to_matching_eager   sN   



ri   c                 C   s&   ~dd | D }dd |D }||fS )Nc                 S   s   g | ]}t |qS r   rZ   r\   r   r   r   r     s    z2convert_to_mixed_eager_tensors.<locals>.<listcomp>c                 S   s   g | ]}|  qS r   )rb   r\   r   r   r   r     s    r   )valuesr$   r?   typesr   r   r   convert_to_mixed_eager_tensors  s   rl   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} nqF|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).r^   )lenrD   rc   ranger:   r`   ra   r_   r   r[   rM   )listsr$   	lists_retrd   rk   rN   r_   jr   r   r   args_to_mixed_eager_tensors  sB   
rs   c                 C   s   t | dot| jdd dkS )Ngraphr%   keras_graph)hasattrgetattrrt   )r   r   r   r   r   H  s   r   )N)"__doc__google.protobufr   tensorflow.core.frameworkr   tensorflow.pythonr   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   tensorflow.python.typesr`   tensorflow.python.utilr	   r)   r-   r1   executer4   r9   rA   rE   rH   rJ   rO   rT   rX   ri   rl   rs   r   r   r   r   r   <module>   s8   
0

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