
    BVhT                     x    d Z ddlZddlmZ  G d de      Zej                  d        Zd Z	 ddZ	d	 Z
d
 Zd Zy)zGradient record utilities.    N)
pywrap_tfec                   .    e Zd ZdZdgZd Zd Zd Zd Zy)VariableWatchera/  A scope that tracks all trainable variable accesses within it.

  This explicitly ignores variables that are not marked as trainable.

  Sample usage:

  var = tf.Variable(0.0)
  with VariableWatcher() as variable_watcher:
    var.assign_add(1.0)

  assert variable_watcher.watched_variables == [var]
  _variable_watcherc                     d | _         y N)r   selfs    N/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/eager/record.py__init__zVariableWatcher.__init__&   s
    !D    c                 8    t        j                         | _        | S r   )r   TFE_Py_VariableWatcherNewr   r	   s    r   	__enter__zVariableWatcher.__enter__)   s    'AACDKr   c                 B    t        j                  | j                         y r   )r   TFE_Py_VariableWatcherRemover   )r
   typvalue	tracebacks       r   __exit__zVariableWatcher.__exit__-   s    ++D,B,BCr   c                 @    t        j                  | j                        S )z7Returns a tuple of variables accessed under this scope.)r   &TFE_Py_VariableWatcherWatchedVariablesr   r	   s    r   watched_variablesz!VariableWatcher.watched_variables0   s    <<   r   N)	__name__
__module____qualname____doc__	__slots__r   r   r   r    r   r   r   r      s&     ##)"D r   r   c               #      K   t        j                         } 	 | st        j                          d | st        j                          yy# | st        j                          w w xY ww)z7Stop all gradient recording (backprop and forwardprop).N)r   TFE_Py_TapeSetIsStoppedTFE_Py_TapeSetStopOnThreadTFE_Py_TapeSetRestartOnThread)
is_stoppeds    r   stop_recordingr%   6   sU      113*1++-	..0 :..0 s   A&A
 A&
A##A&c                 ,    t        j                  |       S )a  Returns true if any tape in the stack watches any of these tensors.

  Only takes GradientTapes into account, not forward accumulators.

  Args:
    tensors: Tensors to check, typically inputs to an operation.

  Returns:
    Boolean, whether any tape watches any of `tensors`.
  )r   "TFE_Py_TapeSetShouldRecordBackprop)tensorss    r   should_record_backpropr)   C   s     
	6	6w	??r   c                 6    t        j                  | ||||       y)z0Records the operation on all tapes in the stack.N)r   TFE_Py_TapeSetRecordOperation)op_typeoutput_tensorsinput_tensorsbackward_functionforward_functions        r   record_operationr1   Q   s     **7N+8:K+;=r   c                 4    t        j                  | |||       y)z9Records the operation on all backward tapes in the stack.N)r   %TFE_Py_TapeSetRecordOperationBackprop)r,   r-   r.   r/   s       r   record_operation_backprop_onlyr4   Y   s     227N3@3DFr   c                 6    t        j                  | ||||       y)a  Records the operation on all forward accumulators in the stack.

  Args:
    op_type: a string for the operation type, used in the backprop code
    output_tensors: a list of Python Tensor objects output by the operation
    input_tensors: a list of input Tensors to the recorded operation
    backward_function: the function to be called to, given the gradients of the
      output tensors, produce the gradients of the input tensors. This function
      is automatically transposed to produce output gradients given input
      gradients.
    forwardprop_output_indices: indicates any output_tensors which contain JVPs.
      Typically these will have come from TFE_Py_PackForwardGradients. May be
      None or an empty sequence if there are no JVP outputs from the operation.
  N)r   (TFE_Py_TapeSetRecordOperationForwardprop)r,   r-   r.   r/   forwardprop_output_indicess        r   !record_operation_forwardprop_onlyr8   a   s    " 55~}.? "r   c                  ,    t        j                          S )z#Returns True if any tape is active.)r   TFE_Py_TapeSetIsEmptyr   r   r   could_possibly_recordr;   w   s    --/	//r   r   )r   
contextlibtensorflow.pythonr   objectr   contextmanagerr%   r)   r1   r4   r8   r;   r   r   r   <module>r@      sW    !  ( f  @ 	1 	1@ '+=F",0r   